Day: September 7, 2016

  • การแปลงข้อมูลในรูปแบบ JSON เป็นคลาส JAVA

    ในการแลกเปลี่ยนข้อมูลระหว่าง application กับ service ที่ติดต่อกับฐานข้อมูลบน server โดยส่วนใหญ่ก็จะแลกเปลี่ยนข้อมูลโดยใช้รูปแบบ JSON ซึ่งในส่วนของแอพพลิเคชัน ต้องทำคลาสในการรับข้อมูลพื่อให้สามารถรับข้อมูลและนำไปใช้ได้สะดวกขึ้น

    ตัวอย่างข้อมูล JSON เช่น

    [

                       {

                                 “point”: “40.266044,-74.718479”,

                                 “homeTeam”:”Lawrence Library”,

                                 “awayTeam”:”LUGip”,

                                 “markerImage”:”images/red.png”,

                                 “information”: “Linux users group meets second Wednesday of each month.”,

                                 “fixture”:”Wednesday 7pm”,

                                 “capacity”:””,

                                 “previousScore”:””

                       },

                       {

                                 “point”:”40.211600,-74.695702″,

                                 “homeTeam”:”Hamilton Library”,

                                 “awayTeam”:”LUGip HW SIG”,

                                 “markerImage”:”images/white.png”,

                                 “information”: “Linux users can meet the first Tuesday of the month to work out harward and configuration issues.”,

                                 “fixture”:”Tuesday 7pm”,

                                 “capacity”:””,

                                 “tv”:””

                       },

                       {

                                 “point”:”40.294535,-74.682012″,

                                 “homeTeam”:”Applebees”,

                                 “awayTeam”:”After LUPip Mtg Spot”,

                                 “markerImage”:”images/newcastle.png”,

                                 “information”: “Some of us go there after the main LUGip meeting, drink brews, and talk.”,

                                 “fixture”:”Wednesday whenever”,

                                 “capacity”:”2 to 4 pints”,

                                 “tv”:””

                       }

    ]

    และตัวอย่างคลาส JAVA ที่เราต้องการสร้างเอาไว้รับข้อมูล ดังนี้ค่ะ

    ———————————-com.example.Example.java———————————–

    package com.example;

    import javax.annotation.Generated;

    @Generated(“org.jsonschema2pojo”)

    public class Example {

    private String point;

    private String homeTeam;

    private String awayTeam;

    private String markerImage;

    private String information;

    private String fixture;

    private String capacity;

    private String previousScore;

    private String tv;

     

    public String getPoint() {

    return point;

    }

    public void setPoint(String point) {

    this.point = point;

    }

    public String getHomeTeam() {

    return homeTeam;

    }

     

    public void setHomeTeam(String homeTeam) {

    this.homeTeam = homeTeam;

    }

    public String getAwayTeam() {

    return awayTeam;

    }

    public void setAwayTeam(String awayTeam) {

    this.awayTeam = awayTeam;

    }

    public String getMarkerImage() {

    return markerImage;

    }

    public void setMarkerImage(String markerImage) {

    this.markerImage = markerImage;

    }

    public String getInformation() {

    return information;

    }

    public void setInformation(String information) {

    this.information = information;

    }

    public String getFixture() {

    return fixture;

    }

    public void setFixture(String fixture) {

    this.fixture = fixture;

    }

    public String getCapacity() {

    return capacity;

    }

    public void setCapacity(String capacity) {

    this.capacity = capacity;

    }

    public String getPreviousScore() {

    return previousScore;

    }

    public void setPreviousScore(String previousScore) {

    this.previousScore = previousScore;

    }

    public String getTv() {

    return tv;

    }

    public void setTv(String tv) {

    this.tv = tv;

    }

    }

    ซึ่งก็คือคลาส ที่มี method –> get กับ set นั่นเอง

    ซึ่งจะเห็นว่าถ้าฐานข้อมูลเรามีหลายฟิลด์ ก็ต้งต้องทำ method get กับ set ทั้งหมดเลย (ร้องหายแปป)

     

    จะดีแค่ไหนถ้ามีตัวสร้างคลาสให้เราจากข้อมูล JSON ของเราเลย   ง่ายๆ ไปดูเลยค่ะที่เว็บ

    http://www.jsonschema2pojo.org/

        การใช้งานก็ง่าย ๆ โดยการวางตัวอย่างข้อมูล JSON ของเราลงไปและระบุค่านิดหน่อยตามต้องการ

    1

    ขอยกตัวอย่างบางตัวหลัก เช่น

    – Package คือ ชื่อตำแหน่งที่เก็บคลาส

    – class name คือ ชื่อคลาส

    – Source Type คือ รูปแบบของ JSON ถ้าเป็นดังตัวอย่างข้อมูลก็กำหนดเป็น JSON

    – Annotation style คือ รูปแบบของคลาสที่สร้าง

    จากนั้นสังเกตด้านล่างมีปุ่ม preview เพื่อดูตัวอย่างคลาสที่สร้าง หรือจะให้ส่งออกเป็นไฟล์ zip ก็ได้เช่นกัน

    2

    จากตัวอย่าง กดปุ่ม Preview แสดงดังรูป

    3

    ขอขอบคุณ

    http://www.jsonschema2pojo.org/

    http://ajbee.me/2015/09/29/web-data-type-part2-json/

    http://codemobiles.com

    http://devahoy.com/posts/android-custom-listview-with-gson-tutorial/