0

This is the JSON result of my query.

{
    "_id":{"$id":"551fb585ecba12c819000032"},
    "nome":"Google","loc":[-122.083983,37.422969],
    "icona":1,
    "istituzione_id":{"$id":"551fb556ecba12c819000031"}
    }
    {
    "_id":{"$id":"5520fe2becba12c003000029"},
    "nome":"Oracle","loc":[-122.262168,37.531595],
    "icona":1,
    "istituzione_id":{"$id":"551fb556ecba12c819000031"}
    }

I've tried to parse JSON result in this manner:

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

           while ((line = reader.readLine()) != null) {
                    Log.e("log_a_line", line);
                sb.append(line + "\n");
            }

           is.close();
            result = sb.toString();
                    Log.e("log_a_result", result);

        } catch (Exception e)
                {
                    Log.e("log_tag_convert", "Error converting result" + e.toString());
                }

        //-----PARSER----
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = -1; i < jArray.length() - 1; i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag", "id:" + json_data.getString("_id"));
            }
        }
        catch (JSONException e)
             {
            Log.e("log_tag_parsing", "Error parsing data" + e.toString());
             }

But after run the app, in my logcat i found error:

log_tag_parsing﹕ Error parsing dataorg.json.JSONException: Value {"icona":1,"loc":[-122.083983,37.422969],"nome":"Google","istituzione_id":{"$id":"551fb556ecba12c819000031"},"_id":{"$id":"551fb585ecba12c819000032"}} of type org.json.JSONObject cannot be converted to JSONArray

I have thought that the problem was about the subarray loc:[....] maybe.

I can't reach the solution. Someone can help me ?

5 Answers 5

1

You're missing a comma between objects; and use [] for arrays and {} for objects.

Next time use jsonlint.com to validate your JSON first.

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

Comments

0

org.json.JSONObject cannot be converted to JSONArray

The answer is in your exception. You are trying to convert jsonobject to jsonArray. Convert your result into JsonObject

Comments

0

see here you are getting JSONObject as a result. but while parsing, you are expecting JSONArray.

Before parsing First add one check to the result.

E.g.

if (resultObj instanceof JSONArray) {
    // process Array
}
else if (resultObj instanceof JSONObject) {
    // processObj
}

Comments

0

Why your for loop have starting value as -1 and change the Log printing line to. See code below:

//-----PARSER----
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = -1; i < jArray.length() - 1; i++) {   //this line
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag", "id:" + json_data.getString("_id"));//this line
            }
        }

change both mentioned line to as:

//-----PARSER----
            try {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {   //this line
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag", "id:" + json_data.getString("$id"));
                }
            }

1 Comment

You have to change at 2 different lines. For the Log printing line the "_id" you have to change it to "$id"
0

Json array syntax should be like this one

employees is array name

[] // for array that contains objects {} between one object and another ,

"employees":[
     {"firstName":"John", "lastName":"Doe"}, 
     {"firstName":"Anna", "lastName":"Smith"}, 
     {"firstName":"Peter","lastName":"Jones"}
 ]

create correct json array

EDIT

to generate array of json

$a = array(
        array('foo' => 'bar'),
        array('foo' => 'baz'));
$json = json_encode($a);

to given

[{"foo":"bar"},{"foo":"baz"}]

read more

http://php.net/manual/en/function.json-encode.php

2 Comments

the json is generated in my php file with this "echo json_encode($row);"
make array and then encode it echo json_encode($arr); which arr is your 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.