1

When i call server its response is based of json object. Actually, I know how to parse JSON object but this response is strange for me. Server response is:

{"body":"Not Available!","clazz":"SoccerMatchPreview","id":{"inc":-2024241794,"machine":415106952,"new":false,"time":1337861978000,"timeSecond":1337861978},"publishedDate":"2012-06-08 17:00:00 +0100","refKey":"SoccerMatchPreview_4fb897be18be8b87f9117595","title":"Poland vs Greece"}

Those Information that I need are body, publishedDate, refKey and title. The code that i have written based of JSON object is this:

JSONObject jObject = new JSONObject(response);
                    JSONArray contestantObjects = jObject.getJSONArray("id");
                    for(int i=0; i<contestantObjects.length(); i++) {
                        mPreview.setBody(contestantObjects.getJSONObject(i).getString("body").toString());
                        mPreview.setPublishedDate(contestantObjects.getJSONObject(i).getString("publishedDate").toString());
                        mPreview.setRefKey(contestantObjects.getJSONObject(i).getString("refKey").toString());
                        mPreview.setTitle(contestantObjects.getJSONObject(i).getString("title").toString());
                    }

But because it doesn't have "[]" I think it's not JSON object. therefore, I wrote another code based JSON array.

JSONArray contestantObjects = new JSONArray(response);
                    for(int i=0; i<contestantObjects.length(); i++) {
                        mPreview.setBody(contestantObjects.getJSONObject(i).getString("body").toString());
                        mPreview.setPublishedDate(contestantObjects.getJSONObject(i).getString("publishedDate").toString());
                        mPreview.setRefKey(contestantObjects.getJSONObject(i).getString("refKey").toString());
                        mPreview.setTitle(contestantObjects.getJSONObject(i).getString("title").toString());
                    }

but result is same and Logcat shows:

Value {"id":{"timeSecond":1337861978,"time":1337861978000,"new":false,"machine":415106952,"inc":-2024241794},"body":"Not Available!","title":"Poland vs Greece","publishedDate":"2012-06-08 17:00:00 +0100","clazz":"SoccerMatchPreview","refKey":"SoccerMatchPreview_4fb897be18be8b87f9117595"} of type org.json.JSONObject cannot be converted to JSONArray

any suggestion would be appreciated. Thanks

1 Answer 1

4
JSONArray contestantObjects = jObject.getJSONArray("id");

Your error is here, id is itself a complex object, not an array.

"id":{"inc":-2024241794,"machine":415106952,"new":false,"time":1337861978000,"timeSecond":1337861978}

Therefore, after getting the id JSON object, you should be able to get the individual attributes, e.g. inc, machine, new, time, and timeSecond.

JSONObject idObject = ...getJSONObject("id");
String machine = idObject.get("machine");

A JSON array data structure would have looked like this: [] signifies an array.

For example, "Animals":["Pig", "Cat", "Dog"].

In another example, it can also be an Array of complex objects, "Animals":[{"name":"AAA", "blood":"A"}, {"name":"BBB", "blood":"B"}].

EDIT: Here is a good JSON visualizer i would recommend.

http://jsonviewer.stack.hu/

enter image description here

4
  • Thanks dear Chin, based on what you mentioned i found that "id" is wrong key because i don't need its items. How to get body, publishedDate, refKey and title? Based of what, should i have to parse "response" string?
    – Hesam
    May 29, 2012 at 2:54
  • To get publishedDate, refKey, and title, you would only need JSONObject jObject = new JSONObject(response); String publishedDate = jObject.get("publishedDate"); and so on.
    – HJW
    May 29, 2012 at 2:58
  • This part, i would strongly recommend that you look into a good JSON library, GSON from Google seems to be a good one. Here is a short tutorial: java.sg/parsing-a-json-string-into-an-object-with-gson-easily
    – HJW
    May 29, 2012 at 3:01
  • Thanks Chin i could parse it.
    – Hesam
    May 29, 2012 at 3:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.