2

I have the following Item class:

public class Item {
    public Object item;
}

I am inserting a JSON into this object using GSON.

tmp =

{
    "_id": {
        "$oid": "5076371389d22e8906000000"
    },
    "item": {
        "values": [
            {
                "value1": [
                    4958,
                    3787,
                    344
                ],
                "value2": [
                    4,
                    13,
                    23
                ]
            }
        ], 
        "name": "item1"
    }
}

Java bit:

Item item = new Item();
Gson g = new Gson();
it = g.fromJson(tmp.toString(), Item.class);

it.item becomes a StringMap type (http://code.google.com/p/google-gson/source/browse/trunk/gson/src/main/java/com/google/gson/internal/StringMap.java?r=1131)

I now need to access the sub-objects within this object. I can use the overridden toString function this type has which prints all objects within this object. But how would I be able to navigate through it? P.S. The reason I put everything into an object datatype not a structured class is that the JSON structure varies each time, so I can't really have a class schema. Any suggestions?

2 Answers 2

2

You should create an object structure that reflects the JSON instead (since this is what you're trying to do anyway). For your example, you could use this:

public class MyObject {
    private Item item;
    private String _id;

    // getters, setters, etc.
}

public class Item {
    private List<Value> values;
    private String name;

    // getters, setters, etc.
}

public class Value {
    private List<Integer> values1;
    private List<Integer> values2;

    // getters, setters, etc.
}

Then pass MyObject.class to Gson:

MyObject myObj = g.fromJson(tmp.toString(), MyObject.class);

You can get the lists in values like so:

List<Integer> values1 = myObj.getItem().getValues().get(0).getValues1();
List<Integer> values2 = myObj.getItem().getValues().get(0).getValues2();

Try that and see if it works.

Also, you should check out my answer to a similar question here, specifically the part at the end about how to write an object structure for Gson based on some JSON object.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I didn't see the end of your question, sorry. How much does the JSON structure vary?
It is generated from an unstructured dataset. The only thing that remains the same is Item object.
0

You can always create a constructor for the custom object that uses reflection and takes the StringMap

public MyObject(StringMap sm){
    Iterator it = sm.entrySet().iterator();
    while(it.hasNext()){
        Entry pairs = (Entry)it.next();
        Class<?> c = this.getClass();
        try {
            Field value = c.getDeclaredField((String) pairs.getKey());
            value.set(this, pairs.getValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Comments

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.