2

I am developing an Android application which fetches data from a server through HTTP GET, stores the response (JSON format) in a String. I want to parse the String and get the values within the JSONArray.

The received JSON which is stored in the STRING is :

{
    "code": 1,
    "data": {
        "moodGraphData": {
            "myself": {
                "1": {
                    "id": "999999999",
                    "value": "4.3",
                    "name": "Myself",
                    "owner": "",
                    "type": 1,
                    "children": []
                }
            },
            "my_teams": [],
            "my_units": [],
            "companies": [
                {
                    "id": "4",
                    "name": "Testing",
                    "owner": "Ankur Ankur",
                    "value": "3.4",
                    "type": "4",
                    "children": {
                        "0": {
                            "id": "10",
                            "value": 3.8,
                            "name": "Mun tiimi",
                            "owner": "",
                            "type": 5,
                            "children": [],
                            "count": 1
                        },
                        "4": {
                            "id": "31",
                            "value": 3.05,
                            "name": "gmail.com",
                            "owner": "",
                            "type": 5,
                            "children": [
                                {
                                    "id": "27",
                                    "value": 2.3,
                                    "name": "Priidu Team2",
                                    "owner": " ",
                                    "type": 5,
                                    "children": [],
                                    "count": 1
                                }
                            ],
                            "count": 2
                        },
                        "6": {
                            "id": "50",
                            "value": 2.95,
                            "name": "gmail.com",
                            "owner": "",
                            "type": 5,
                            "children": [
                                {
                                    "id": "51",
                                    "value": 2.6,
                                    "name": "Testing",
                                    "owner": "Ujjwal Mairh",
                                    "type": 5,
                                    "children": [],
                                    "count": 1
                                }
                            ],
                            "count": 2
                        }
                    }
                }
            ]
        },
        "companyTopStatements": [
            {
                "id": "1",
                "text": "I am happy at work today",
                "key": "Mood",
                "answer": "3.5"
            },
            {
                "id": "8",
                "text": "I am using my strengths at work",
                "key": "Strengths",
                "answer": "3.5"
            },
            {
                "id": "11",
                "text": "My opinions matter and I can influence in my work",
                "key": "Influence",
                "answer": "3.5"
            }
        ],
        "companyBottomStatements": [
            {
                "id": "13",
                "text": "I am surrounded by teammates who are motivated and doing great things",
                "key": "Team",
                "answer": "3.2"
            },
            {
                "id": "14",
                "text": "I have a close friends at work",
                "key": "Friendship",
                "answer": "3.2"
            },
            {
                "id": "15",
                "text": "I receive frequent feedback in order to progress in my role",
                "key": "Feedback",
                "answer": "3.2"
            }
        ]
    },
    "errors": null
}

I want to parse the JSON Array "companyTopStatements", and retrieve the value in "key", which is inside a JSONObject.

The code I used is as follows :

        JSONObject jsonObj = new JSONObject(Get_MyCompany_JSON);
        JSONArray jsonArray = jsonObj.getJSONArray("companyTopStatements");
        for(int i=0;i<jsonArray.length();i++)
        {
          JSONObject curr = jsonArray.getJSONObject(i);
                      String keyValue = curr.getString("key");
          Log.d("Key",keyValue);
        }

I am unable to parse it and obtaining an exception :

04-08 21:25:03.561: W/System.err(13464):        org.json.JSONException: No value for companyTopStatements
04-08 21:25:03.561: W/System.err(13464):    at org.json.JSONObject.get(JSONObject.java:354)
04-08 21:25:03.561: W/System.err(13464):    at org.json.JSONObject.getJSONArray(JSONObject.java:548)
04-08 21:25:03.561: W/System.err(13464):    at com.moodwonder.fi.HTTP_SignIn_Thread.doInBackground(HTTP_SignIn_Thread.java:105)
04-08 21:25:03.561: W/System.err(13464):    at com.moodwonder.fi.HTTP_SignIn_Thread.doInBackground(HTTP_SignIn_Thread.java:1)
04-08 21:25:03.561: W/System.err(13464):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-08 21:25:03.561: W/System.err(13464):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-08 21:25:03.561: W/System.err(13464):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-08 21:25:03.561: W/System.err(13464):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-08 21:25:03.561: W/System.err(13464):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-08 21:25:03.566: W/System.err(13464):    at java.lang.Thread.run(Thread.java:841)

2 Answers 2

4
JSONObject jsonObj = new JSONObject(Get_MyCompany_JSON);
JSONObject jsondata = jsonObj.getJSONObject("data");
JSONArray jsonArray = jsondata.getJSONArray("companyTopStatements");
for(int i=0;i<jsonArray.length();i++)
{
  JSONObject curr = jsonArray.getJSONObject(i);
  String keyValue = curr.getString("key");
  Log.d("Key",keyValue);
 }

You forgot companyTopStatements is in data object

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

1 Comment

Thank you. Your solution fixed it. I could'nt read the closing brace of data-object properly. I assumed that companyTopStatements is a stand alone JSONArray.
0

org.json.JSONException: No value for companyTopStatements

You have a json object node at the top but JSONArray companyTopStatements is not under that json object.

{ // json object node 
    "code": 1,
    "data": { // json object data 

Under data you have

"companyTopStatements": [

You have the JSONArray companyTopStatements under JSONObject data.

You can confirm by pasting the json @

http://www.jquery4u.com/demos/online-json-tree-viewer/#

 JSONObject jsonObj = new JSONObject(Get_MyCompany_JSON);
 JSONObject jb = jsonObj.getJSONObject("data");
 JSONArray jsonArray = jb.getJSONArray("companyTopStatements");

1 Comment

Thank you very much. I assumed that companyTopStatements is a stand alone JSONArray. The JSON tree View really helps in getting it right.

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.