-2

I get a JSON in following format

{
    "goods" : {
        "t1" : {
            "name" : "name1"
        },
        "t2" : {
            "name" : "name2"
        }
    }
}

In PHP and Angular that works fine

In android studio I get the following error

W/System.err: org.json.JSONException: Unterminated array at character 23 of {
W/System.err:   "goods" : [
W/System.err:       "t1" : {
W/System.err:           "name" : "name1"
W/System.err:       },
W/System.err:       "t2" : {
W/System.err:           "name" : "name2"
W/System.err:       }
W/System.err:   ]
W/System.err: }

It works fine When in the following structure

{
    "goods" : [
        {
            "name" : "name1"
        },
        {
            "name" : "name2"
        }
    ]
}

But the problem is I get a JSON from the Server in : many objects in object, not : many objects in Array. Is there any way to make this work, i can't change the structure from the server, because on that Json works many other web sites. Please help !!!!!!!!!!!!!

Here is code how i use it

    try {
        JSONObject jsonObj = new JSONObject(jsonStr);


        // Getting JSON Array node
        JSONArray contacts = jsonObj.getJSONArray("goods");

        // looping through All Contacts
        for (int i = 0; i < contacts.length(); i++) {
            JSONObject c = contacts.getJSONObject(i);
            String name = c.getString("name");

            Log.e("JSON : ", name);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
Asylzat
  • 197
  • 5
  • 17
  • 1
    Voting to close as not reproducible/typo: The JSON you've shown after "In android studio I get the following error" is not the same JSON you've shown at the top of the question. And unlike the JSON at the top of the question, it's invalid. Note the `[` rather than `{` after `"goods" :` in your second one. So where you're getting the error, you've just fed it invalid JSON. Use the valid version (at the top of the question) instead. – T.J. Crowder Mar 03 '17 at 09:14
  • post your code which you try for this – Zaki Pathan Mar 03 '17 at 09:19
  • Json Array doesnt have keys, just values you can iterate. So you need to create simple json object, not array. – Vygintas B Mar 03 '17 at 09:22
  • JSONArray contacts = jsonObj.getJSONArray("goods"); replace this with JSONArray contacts = jsonObj.getJSONObject(0); and try – Zaki Pathan Mar 03 '17 at 09:27
  • I was wondering is there any other way to make it work "invalid JSON", maybe other libraries than import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; – Asylzat Mar 03 '17 at 09:28
  • @AsylzatAzaev please check my answer. check link also – Zaki Pathan Mar 03 '17 at 09:31

1 Answers1

0
try {
        JSONObject jsonObj = new JSONObject(jsonStr);


    // Getting JSON Object node
    JSONObject contacts = jsonObj.getJSONObject("goods");

    // looping through All Contacts
    for (int i = 0; i < contacts.length(); i++) {
        JSONObject c = contacts.getJSONObject(i);
        String name = c.getString("name");

        Log.e("JSON : ", name);

    }

} catch (JSONException e) {
    e.printStackTrace();
}

try this. If this doesn't work feel free to ask or try this hope it helps you

EDIT:

If your keys are dynamically generated. Please try below code

try
        {   

            JSONObject jObject= new JSONObject(responseData).getJSONObject("goods");
            Iterator<String> keys = jObject.keys();
            while( keys.hasNext() )
            {
                String key = keys.next();
                Log.v("**********", "**********");
                Log.v("category key", key);
                JSONObject innerJObject = jObject.getJSONObject(key);

                String name = innerJObject.getString("name");


                Log.v("name = "+name);

            }
        }
        catch (JSONException e){
           e.printStackTrace();    
        }
Community
  • 1
  • 1
Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26