1

I have to parse a JSON string returned by a webService in a structure like this :

{
"catList" : {
"1" :{
    "name" : "NAME",
    "qty" : "QTY",
    "groupList" : {
        "40" : {
        "name" : "NAME",
        "qty" : "QTY",
        "subjectList" : {
            "1" : {
            "name" : "NAME",
            "qty" : "QTY",
            },
            "5" : {
            "name" : "NAME",
            "qty" : "QTY",
            },
            ...
        },
        "45" : {
        "name" : "NAME",
        "qty" : "QTY",
        "subjectList" : {
            "23" : {
            "name" : "NAME",
            "qty" : "QTY",
            },
            "45" : {
            "name" : "NAME",
            "qty" : "QTY",
            },
            ...
        },
        ...
"2" :{
    "name" : "NAME",
    "qty" : "QTY",
    "groupList" : {
        "142" : {
        "name" : "NAME",
        "qty" : "QTY",
        "subjectList" : {
            "86" : {
            "name" : "NAME",
            "qty" : "QTY",
            },
        "23" : {
        "name" : "NAME",
        "qty" : "QTY",
            },
            ...
        },
        "7" : {
        "name" : "NAME",
        "qty" : "QTY",
        "subjectList" : {
            "98" : {
            "name" : "NAME"
            "qty" : "QTY"
            },
            "08" : {
            "name" : "NAME"
            "qty" : "QTY"
            }
        },
        ...
    },
    ...
"3" :{SAME OTHERS},
"4" :{SAME OTHERS},
"5" :{SAME OTHERS},
"6" :{SAME OTHERS},
"7" :{SAME OTHERS},
"8" :{SAME OTHERS},
"9" :{SAME OTHERS},
}

I dont know how?! I have some Classes like these :

public class CatList {

    private String name;
    private String qty;
    private JSONObject groupList;
}

public class GroupList {

    private String name;
    private String qty;
    private JSONObject subjectList;
}

public class SubjectList {

    private String name;
    private String qty;
}

But because groupLists and subjectList have random title I dont know how to recognize them. Is This possible؟ How can I do? please help

Amin
  • 11
  • 1
  • 3
  • What do you mean groupLists and subjectList have random titles? As in, the JSON key changes? Because that would be odd behaviour.. – Justin Jasmann Jan 17 '13 at 19:50
  • Here there are many answers on how to perform the parsing: http://stackoverflow.com/questions/3494328/parsing-json-in-android – Gabriel Jan 17 '13 at 19:53
  • Yeah that's definitely weird. I would advise the same as agamov below. JSON data from the server is supposed to be consistent and follow a single, agreed upon design. – Justin Jasmann Jan 17 '13 at 19:57
  • @jackgris I know how to parse JSON in general, but In this specific case I'm confused – Amin Jan 17 '13 at 19:58
  • Refer to this answer : http://stackoverflow.com/questions/7304002/how-to-parse-a-dynamic-json-key-in-a-nested-json-result Though it seems a little messy to me, unfortunately I think it's your only option. – Justin Jasmann Jan 17 '13 at 20:02
  • @Amin Here is a short tutorial on the subject: http://bit.ly/yhMka1 – Karey Powell Jan 17 '13 at 20:02
  • @JustinJasmann I recommend them to change lists from JSONOBJECT to JSONARRAY but they said it is not STANDARD – Amin Jan 17 '13 at 20:02

6 Answers6

3

I would advice you to change the JSON structure returned by server because this is bad design :)

Anyways, here is my working example using Jackson library. Sample json lives here: https://dl.dropbox.com/u/6129677/json_test.json Note that I have removed "catList", and just left the list of categories (see json).

Category.java:

public class Category {

    private String name;
    private String qty;
    private Map<String, Group> groupList;

    @JsonProperty("groupList")
    public Map<String, Group> getGroupList() {
        return groupList;
    }

    @JsonProperty("groupList")
    public void setGroupList(Map<String, Group> groupList) {
        this.groupList = groupList;
    }

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("qty")
    public String getQty() {
        return qty;
    }

    @JsonProperty("qty")
    public void setQty(String qty) {
        this.qty = qty;
    }
}

Group.java

public class Group {

    private String name;
    private String qty;

    private Map<String, Subject> subjectList;

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("qty")
    public String getQty() {
        return qty;
    }

    @JsonProperty("qty")
    public void setQty(String qty) {
        this.qty = qty;
    }

    @JsonProperty("subjectList")
    public Map<String, Subject> getSubjectList() {
        return subjectList;
    }

    @JsonProperty("subjectList")
    public void setSubjectList(Map<String, Subject> subjectList) {
        this.subjectList = subjectList;
    }
}

Subject.java

public class Subject {
    private String name;
    private String qty;

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("qty")
    public String getQty() {
        return qty;
    }

    @JsonProperty("qty")
    public void setQty(String qty) {
        this.qty = qty;
    }
}

Marshalling JSON to our Objects:

try {
    ObjectMapper om = new ObjectMapper();
    Map<String, Category> catMap = om.readValue(getAssets().open("json_test.json"), new TypeReference<Map<String, Category>>() {
    });
} catch (IOException e) {
    e.printStackTrace();
}

As a result you will get Map object.

agamov
  • 4,407
  • 1
  • 27
  • 31
  • Unfortunately, I have no control over it – Amin Jan 17 '13 at 20:00
  • are you sure you have this kind of json response? It is a HUGE object with nested objects with nested objects with nested objects :) It would be better to have the catlist as array of cats, grouplist as array of groups, subjectlist as array of subjects. – agamov Jan 17 '13 at 20:07
0

Do you know http://json.org/java/ project.

I use this lib in a web app, and it may apply in your case.

Fred
  • 116
  • 6
0

I suggest that you use Jackson in your project and make a class type for the JSON you are trying to deserialize.

ecbrodie
  • 11,246
  • 21
  • 71
  • 120
0

And if you go through the array of JSON objects and take the name with the getName() method? Or also, use the library http://jackson.codehaus.org/?

Gabriel
  • 476
  • 2
  • 11
  • 64
0

Use jackson because of performance and simplicity.

EvZ
  • 11,889
  • 4
  • 38
  • 76
-1

I'll post this answer here just so it's clear and not hidden in the comment section.

JSONObject jsonObject = search.getJSONObject("object_key");
Iterator jsonObjectKeys = jsonObject.keys();

while(jsonObjectKeys.hasNext()) {
    String currentDynamicKey = (String)keys.next();
    JSONObject currentDynamicValue = jsonObject.getJSONObject(currentDynamicKey);

    //do what you need to do with the data here
}
Justin Jasmann
  • 2,363
  • 2
  • 14
  • 18