简体   繁体   中英

Java json getting data from url to jcombobox

İ'm trying to develop swing app on netbeans.İ'm using gson to decode json.İ get json data from php site. İ couldn't convert json data to java array.

How could i convert getting json file to java array. And then İ need to set jcombobox datasource this array.

My json file like this on url

[
    {
        "id": "1",
        "ad": "jack",
        "latitude": "41.0000",
        "longitude": "32.000",
        "speed": "",
        "hour": "01:28:56",
        "day": "18.04.2016",
        "adres": "adres",
        "resimyol": "http:\/\/maps.google.com\/maps\/api\/staticmap?.jpg"
    },
    {
        "id": "2",
        "ad": "Abraham",
        "latitude": "41.0000",
        "longitude": "41.0000",
        "speed": "",
        "hour": "01:28:56",
        "day": "18.04.2016",
        "adres": "adres",
        "resimyol": "http:\/\/maps.google.com\/maps\/api\/staticmap?.jpg"
    }
]

And here my java files

public class JsonPojo {
    public String id;
    public String ad;
    public String latitude;
    public String longitude;
    public String speed;
    public String hour;
    public String day;
    public String adres;
    public String resimyol;

    public String getId() { return id; }
    public String getName() { return ad; }
    public String getlat() { return latitude; }
    public String getlon() { return longitude; }
    public String getspeed() { return speed; }
    public String gethour() { return hour; }
    public String getday() { return day; }
    public String getadres() { return adres; }
    public String getresim() { return resimyol; }
}

public static void main(String args[]) {
    // got json here...

    Gson gson = new Gson();    

    JsonPojo[] array = gson.fromJson(gson, JsonPojo[].class); 
    JsonPojo obj = new Gson().fromJson(json, JsonPojo.class);

    System.out.println("ID: " +obj.getId());
    System.out.println("ID: " +obj.getName());
    System.out.println("ID: " +obj.getlat());
    System.out.println("ID: " +obj.getlon());
    System.out.println("ID: " +obj.getspeed());
    System.out.println("ID: " +obj.gethour());
    System.out.println("ID: " +obj.getday());
    System.out.println("ID: " +obj.getadres());
    System.out.println("ID: " +obj.getresim());
}

When am i trying to run project its give me error like this

Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 2 column 13 path $
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
    at com.google.gson.Gson.fromJson(Gson.java:887)
    at com.google.gson.Gson.fromJson(Gson.java:852)
    at com.google.gson.Gson.fromJson(Gson.java:801)
    at com.google.gson.Gson.fromJson(Gson.java:773)
    at KonumGoster$1.run(KonumGoster.java:145)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 2 column 13 path $
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
    ... 19 more
BUILD SUCCESSFUL (total time: 5 minutes 5 seconds)

The problem is you are trying to pass the gson object as the JSON. And you also don't need the second call to try to to get a single JsonPojo , that will not work.

Gson gson = new Gson();    

JsonPojo[] array = gson.fromJson(gson, JsonPojo[].class);
JsonPojo obj = new Gson().fromJson(json, JsonPojo.class);

It should be like this:

Gson gson = new Gson();    

JsonPojo[] array = gson.fromJson(json, JsonPojo[].class);

You will have an array of your objects and you can iterate over them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM