4

I have a nested JSONArray. I'm new with gson. I have been tried many tutorials but those wasn't helpful. Edit :

[
{
    "DivisionID": "2c0e9dc1-a6a7",
    "DivisionName": "Qwerty",
    "SubDivision": [
        {
            "SubDivisionID": "70c3ac53-eec6",
            "SubDivisionName": "QW123",
            "Vehicle": [
                {
                    "Nopol": "00571564",
                    "LastUpdate": "Oct 10 2010 10:10AM",
                    "LastSpeed": 0,
                    "LastLon": 106.82176,
                    "Location": "KNOWHERE"
                },
                {
                    "Nopol": "352848020936627",
                    "LastUpdate": "Oct10201010: 10AM",
                    "LastSpeed": 0,
                    "LastLon": 10124.228,
                    "Location": "KNOWHERE2"
                }
            ]
        }
    ]
}
]

This is how i tried so far. EDIT :

    public class Post {
@SerializedName("DivisionID")
private String divisionid;
@SerializedName("DivisionName")
private String divisionname;
@SerializedName("SubDivision")
private ArrayList<SubDivision> subdivisions;

public Post(String divisionid, String divisionname) {
this.divisionid = divisionid;
this.divisionname = divisionname;
}
// getter and setter ...

public class SubDivision {
@SerializedName("SubDivisionID")
private String subdivisionid;
@SerializedName("SubDivisionName")
private String subdivisionname;
@SerializedName("Vehicle")
private ArrayList<Vehicles> vehicles;

public SubDivision (ArrayList<Vehicles> vehicles) {
    this.vehicles = vehicles;
}
// getter and setter ...

public class Vehicles {
@SerializedName("Nopol")
private String nopol;
@SerializedName("LastLon")
private String lastlon;
@SerializedName("LastUpdate")
private String lastupdate;
@SerializedName("Location")
private String location;

public Vehicles(String nopol, String lastlon, String lastupdate, String location) {
    this.nopol = nopol;
    this.lastlon = lastlon;
    this.lastupdate = lastupdate;
    this.location = location;
}
// getter and setter ...

this is how i parse it. EDIT :

Type listType = new TypeToken<ArrayList<Post>>(){}.getType();
            beanPostArrayList = new GsonBuilder().create().fromJson(reader, listType);
            postList=new StringBuffer();
            for(Post post: beanPostArrayList){
                Log.d("topic asd: ", post.getDivisionid()+"");
               postList.append("\n id: "+post.getDivisionid()+
                       "\n divname: "+post.getDivisionname());

                Type listType2 = new TypeToken<ArrayList<SubDivision>>(){}.getType();
                SubdivArrayList = new GsonBuilder().create().fromJson(reader, listType2);
                postList2 = new StringBuffer();
                for(SubDivision subdiv: SubdivArrayList){
                    postList.append("\n id: "+subdiv.getSubdivisionid()+
                            "\n subdivname: "+subdiv.getSubdivisionname());

                    Type listType3 = new TypeToken<ArrayList<Vehicles>>(){}.getType();
                    vehicleArrayList = new GsonBuilder().create().fromJson(reader, listType3);
                    postList3 = new StringBuffer();
                    for(Vehicles vehic: vehicleArrayList){
                        postList.append("\n nopol: "+vehic.getNopol()+
                                "\n lastlon: "+vehic.getLastLon()+
                                "\n latupdate: "+vehic.getLastUpdate()+
                                "\n location: "+vehic.getLocation());
                    }
                }
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressDialog.dismiss();
            txtPostList.setText(postList);
            txtSubdivList.setText(postList2);
            txtVehicList.setText(postList3);
        }
    }.execute();

The problem is i don't know how to parse this structure. How can i do it?

1
  • copy { "Nopol": "352848020936627", "LastUpdate": "Oct 10 2010 10:10AM", "LastSpeed": 0, "LastLon": 10124.228 "Location": "KNOWHERE2" } and paste to jsonlint.com to check valid jsons Commented Dec 17, 2014 at 12:32

3 Answers 3

3

The following should work:

public class Example {

    public static void main(String[] args) {

        String s = ""; // THE JSON FROM THE NETWORK
        Gson gson = new Gson();
        Post[] posts = gson.fromJson(s, Post[].class);
        for( Post p : posts ){
            System.out.println(posts.toString() );
        }
    }

    public static class Post {

        @SerializedName("DivisionID")
        String divisionId;

        @SerializedName("DivisionName")
        String divisionName;

        @SerializedName("SubDivision")
        List<SubDivision> subDivisions;
    }

    public static class SubDivision {

        @SerializedName("SubDivisionID")
        String subDivisionId;

        @SerializedName("SubDivisionName")
        String subDivisionName;

        @SerializedName("Vehicle")
        List<Vehicle> vehicles;
    }

    public static class Vehicle {

        @SerializedName("Nopol")
        String nopol;

        @SerializedName("LastUpdate")
        String lastUpdate; // should be a date!

        @SerializedName("LastSpeed")
        String lastSpeed;

        @SerializedName("LastLon")
        Double lastLon;

        @SerializedName("Location")
        String location;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you and sorry, my bad i just typo my question. I've used both codes 'Post[]' and 'TypeToken', its working when i using just one jsonarray not in nested jsonarray.
No need to use TypeToken. Just use gson.fromJson(string, Post[].class); and you are set!
0

Please correct your JSON first

[
        {
            "DivisionID": "2c0e9dc1-a6a7",
            "DivisionName": "Qwerty",
            "SubDivision": [
                {
                    "SubDivisionID": "70c3ac53-eec6",
                    "SubDivisionName": "QW123",
                    "Vehicle": [
                        {
                            "Nopol": "00571564",
                            "LastUpdate": "Oct 10 2010 10:10AM",
                            "LastSpeed": 0,
                            "LastLon": "106.82176",
                            "Location": "KNOWHERE"
                        },
                        {
                            "Nopol": "352848020936627",
                            "LastUpdate": "Oct10201010: 10AM",
                            "LastSpeed": 0,
                            "LastLon": "1014.228",
                            "Location": "KNOWHERE2"
                        }
                    ]
                }
            ]
        }
    ]

Comments

0

First you need to have a valid JSON. Use the below link to format and validate your JSON. http://jsonformatter.curiousconcept.com/

Please use the below corrected JSON:
[
        {
            "DivisionID": "2c0e9dc1-a6a7",
            "DivisionName": "Qwerty",
            "SubDivision": [
                {
                    "SubDivisionID": "70c3ac53-eec6",
                    "SubDivisionName": "QW123",
                    "Vehicle": [
                        {
                            "Nopol": "00571564",
                            "LastUpdate": "Oct 10 2010 10:10AM",
                            "LastSpeed": 0,
                            "LastLon": "106.82176",
                            "Location": "KNOWHERE"
                        },
                        {
                            "Nopol": "352848020936627",
                            "LastUpdate": "Oct10201010: 10AM",
                            "LastSpeed": 0,
                            "LastLon": "1014.228",
                            "Location": "KNOWHERE2"
                        }
                    ]
                }
            ]
        }
    ]

Import the below classes to parse your JSON responses,

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

1 Comment

If it solver your problem then accept as answer for your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.