0
[{"name":"Mark","Surname":"Gaux"}]
[{"Job":"2","Type":"Office"}]

I have this JSON echoed by a PHP file on the server and then received by my Android application.

Since I didn't manage to merge the two into one array [ ], you notice that I have two arrays [] [].

When I had one array I parsed it in my Android application using this loop:

try{

    JSONArray jArray = new JSONArray(response);

            for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                jname = json_data.getString("name");
                jsurname = json_data.getString("Surname");
...
}

Just a simple example.

I'm a bit confused concerning JSONArray and JSONObject. JSONArray is anything between [] and JSONObject is anything between {} ?

Well now that I have two arrays, how do I loop from one into the other?

Or am I better off merging them into one array from the PHP and using the technique I used up to now?

1 Answer 1

1

If you're trying to send both arrays in one response you could do that:

[                                         
    [{"name":"Mark","Surname":"Gaux"}],
    [{"Job":"2","Type":"Office"}]
]

Whole JSON string is a JSONArray:
getJSONArray(0) contains JSONObjects with name and Surname
getJSONArray(1) contains JSONObjects with Job and Type

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

5 Comments

I'm going to try that. You are probably right because before when I tried jArray.toString() it returned only the contents of the first []. so now it should return the contents of the big [] but then how do I read the contents of the other 2 [] [] inside of the big one?
You shouldn't call toString. You should call getJSONObject(index) on JSONArray.
Yea I know, I was just checking the contents of jArray.
Are you sure that is how it works? Because what I see is an array with two other arrays in it and THEN each array has one object. so if you say getJSONArray(0) there really is no JSONObject there but there is an Array. no?
Yes, sure. Two arrays in the array and those two arrays contain the object(s) you want to read. If you need something else you should make it more clear in your question.

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.