I am trying to parse json string in google app engine(java). The problem is sometimes the json schema is not defined well. I have to try/catch things.
For eg:
{
"data":"some data as string"
}
Here the "data" is a string.
{
"data": ["data1","data2"]
}
Now it's an array. I know the JSON has to be in proper defined schema. But that's the limitation i want to overcome. I can try catch things using json.org library. But don't know how to do that with GSON.
I am open to any advice / suggestion. Thanks already. Also do let me know if I am doing things completely wrong.
EDIT :
Using json.org, we can parse the above json like this :
JSONObject jObject = new JSONObject(jsonString);
List<String> mDataList = new ArrayList<String>();
try {
mDataList.add(jObject.getString("data"));
catch(Eception e) {
JSONArray jArray = jObject.getJSONArray("data");
// iterate through the jArray and add it to list
}
Here the json is parsed without caring whether "data" element is array or string. I need to do the same thing with GAE as well. So you can help me with two things,
- How to include dependency of json.org library in GAE project (gradle)
- Or how to parse the data with GSON (Using POJO classes is out of scope here. Because there are lot of elements with uncertainty in the schema)