0

I tried to parse multiple JSON arrays that my server returns to my application.

The structure of the returned message from the server looks like this:

[ json objects ][ json objects ]

returned message :

[{"latitude":"44.33","longitude":"44.33","name":"test1","Notification":"true"}][{"latitude":"44.33","longitude":"44.33","name":"test2","Notification":"false"}]

Now I tried to parse this and get the Notification status (true / false) but I did not get the item i needed.

  try {

       JSONArray jr = new JSONArray(answer);
       JSONObject jb = (JSONObject)jr.getJSONObject(0);
       JSONArray nof = jb.getJSONArray("Notification");
       Log.d("Json", String.valueOf(nof));
  }catch(Exception e)
  {
       e.printStackTrace();
  }

I would be happy if someone can help me to understand what I need todo to achieve my mission.

7
  • You can rather use GSON for parsing, it is much easier. Use GsonFormatter Android studio plug-in for creating model classes Commented Mar 24, 2017 at 8:24
  • what is answer in your first line? and do you get any error? Commented Mar 24, 2017 at 8:24
  • Yes I get the next error - " Value true at Notification of type java.lang.String cannot be converted to JSONArray " Commented Mar 24, 2017 at 8:27
  • Your return message is not a valid JSON. Try wrapping those arrays in an array with a key. Like this: hastebin.com/amohisened.json Commented Mar 24, 2017 at 8:27
  • You should use String nof = jb.getString("Notification"); instead of JSONArray nof = jb.getJSONArray("Notification"); and to print you can simply print value of nof like Log.d("Json", nof); Commented Mar 24, 2017 at 8:31

3 Answers 3

2

Your Json response is invalid...

This is the valid JSON:

  [
    [{
        "latitude": "44.33",
        "longitude": "44.33",
        "name": "test1",
        "Notification": "true"
    }],
    [{
        "latitude": "44.33",
        "longitude": "44.33",
        "name": "test2",
        "Notification": "false"
    }]
  ]

to parse it:

try{
JSONArray json = new JSONArray(answer);
for(int i=0;i<json.length();i++){
     JSONArray array = json.getJSONArray(i);
         for(int i=0;i<array.length();i++){
            JSONObject object = array.getJSONObject(i);
            String Notification = object.getString("Notification");

        }
    }
 }
 catch (JSONException e){
       e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

6 Comments

he is getting error when trying to get value of Notification , exception should be something else if it is invalid json. i thinks it's mistake in posting question , see comments in question
that why i said...it is not a valid JSON
I try this but in the line " JSONArray array = json.getJSONArray(i); " I get the error - " Value at 0 of type org.json.JSONObject cannot be converted to JSONArray "
okay in your response u r getting ,,a JSON object...post the full response...the response u posted on question is wrong!
The problem is the size of the array when I check him is 1 and it need to be 2 ( json.length() ) so in your code it's get only the first element and that make the problem. how I get all the elements of the array ?
|
0

That Json response can't be parsed as a whole. Formally it is not json valid, there are two json arrays that concatenate like that do not make a valid json.

You can wrap those array in a unique json object and that object is now parsable with your code.

{"array1":[],"array2":[]}

Then you can instantiate the jsonObject and get each array separately with their name

2 Comments

Thanks, but how I can convert my correct return string to the type like you answer ?
It depends on the language you are using on the server side to generate that json
0

Create a POJO of Response lets say Notification.java

JSONArray resultArray = response.getJSONArray("result");// your json response 

// using Gson Library By google

compile 'com.google.code.gson:gson:2.7.0'

List<Notification> notificationList = new    Gson().fromJson(String.valueOf(resultArray),Notification.class);

Comments

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.