Search code examples
javajsonswinggsonnetbeans-platform

Java gson convert error


İ'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 file

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[]) {
public void run() {
            new KonumGoster().setVisible(true);
             String json = null;
            try {
                json = readUrl("http://url.com/"
                        + "json.php");
            } catch (Exception ex) {
                Logger.getLogger(KonumGoster.class.getName()).log(Level.SEVERE, null, ex);
            }

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

run: 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)


Solution

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

    The second statement uses gson instead of json as the first argument of Gson::fromJson. If you change this to json, GSON will do its job correctly.

    And the error message in your build comes from the third statement, where you are trying to read a single oject out of a JSON array of objects. You only need the second statement and then iterate over its result to print each item.

    Gson gson = new Gson();
    JsonPojo[] array = gson.fromJson(json, JsonPojo[].class);
    for (JsonPojo obj : array) {
        System.out.println("id: " + obj.getId());
        // etc
    }