0

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,

  1. How to include dependency of json.org library in GAE project (gradle)
  2. 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)
Riyaz Ahamed
  • 802
  • 8
  • 14
  • I'm not clear what the issue is, all standard json library should be able to handle both cases you mentioned – marcadian Dec 03 '15 at 20:05
  • I am sorry for not being clear. I know how to handle this case with json.org But it is not available in GAE. I don't know how to include dependency for that lib. Can you explain how to handle this in GSON? – Riyaz Ahamed Dec 03 '15 at 20:08
  • What have you tried so far? Also check the [GSON User Guide](https://github.com/google/gson/blob/master/UserGuide.md) – Josh J Dec 03 '15 at 20:50
  • @Josh J : I have updated my question. – Riyaz Ahamed Dec 03 '15 at 21:26
  • 1
    I was under the impression that you could include the dependency of `org.json` in your gradle file and then be OK. Similar to this question's answer http://stackoverflow.com/questions/28588802/android-studio-cannot-resolve-import-org-json-jsonobject – Josh J Dec 03 '15 at 21:41

0 Answers0