1

can some one please get me the json parsing for an json array

 line = "contexts": [
    {
        "uuid": "6686feaa-a254-42ec-a662-c36f70f7a586",
        "name": "School",
    },
    {
        "uuid": "bd8e6c44-d461-4bbe-8946-a3717dc7fa7f",
        "name": "Teaching",
    }]

I need uuid and name into to string arrays. I tried

String[] x = new String[10];
String[] y = new String[10];
JSONArray jArray = new JSONArray(line);
for(int i=0;i<jArray.length();i++){          
    JSONObject json_data = jArray.getJSONObject(i);
    x = json_data.getString("name");
y = json_data.getString("uuid");            
        }

I get type mismatch error when I run this. The type of line is string which I return from server.

2 Answers 2

4

use i to add elements to Array's:

String[] x = new String[10];
String[] y = new String[10];
JSONObject json=new JSONObject(line);
JSONArray jArray =json.getJSONArray("contexts");

for(int i=0;i<jArray.length();i++){          
    JSONObject json_data = jArray.getJSONObject(i);
    x[i] = json_data.getString("name");
    y[i] = json_data.getString("uuid");            
        }

you can use http://jsonviewer.stack.hu/ for checking json is valid or not. and use ArrayList instead of Array for getting items dynamically from web service

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

1 Comment

@user1494754 : most welcome friend!!!:) . as i suggest u don't use String Array for getting data from web service. you can use any Collection for it like ArrayList,List,HashMap.
3

Its neither JSONObject, nor JSONArray, return string from server like below:

line = {"contexts": [
    {
        "uuid": "6686feaa-a254-42ec-a662-c36f70f7a586",
        "name": "School",
    },
    {
        "uuid": "bd8e6c44-d461-4bbe-8946-a3717dc7fa7f",
        "name": "Teaching",
    }]}

and then parse like below code:

JSONObject json=new JSONObject(line);
JSONArray jArray =json.getJSONArray("contexts");

1 Comment

Now thats perfect, every Json must be start with an Object { or with an Array [.

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.