简体   繁体   中英

org.json.JSONException in Android while Parsing JSON data

In android studio I am trying to parse JSON data in my Android application. For that I have written this code (given below). But this code is throwing org.json.JSONException . Actually I want output like this:

/*This is just a sample*/
ROW: 1
id = ###
date = 0/0/0
latitude = 12.123456789
longitude = 12.123456789
description = any note

My Code:

String jsonData = "["+
                      "{ "_id" : { "$oid" : "57###ad"} , "document" : { "date" : "3/8/2016" , "latitude" : "33.63043627520588" , "longitude" : "72.95956905931234" , "description" : "note-1"} , "safe" : true} , "+
                      "{ "_id" : { "$oid" : "57###65"} , "document" : { "date" : "7/7/2017" , "latitude" : "33.647092700303" , "longitude" : "72.99582783132792" , "description" : "note-2"} , "safe" : true} "+
                  "]";

JSONObject  jsonRootObject = new JSONObject(jsonData);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("document");
//Iterate the jsonArray and print the info of JSONObjects

for(int i=0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    String date = jsonObject.optString("date").toString();
    String latitude = jsonObject.optString("latitude").toString();
    String longitude = jsonObject.optString("longitude").toString();
    String description = jsonObject.optString("description").toString();
    String id = jsonObject.optString("id").toString();

    result += "ROW"+i+" : \n id= "+ id + \n date= "+ date +" \n latitude= "+ latitude+" \n longitude= "+ longitude +" \n ";
}

Log.d("RESULT:",result);

Error

05-04 23:56:48.475 9762-11393/com.example.abc.project1 E/Error: Exception
                                                                org.json.JSONException: Value [{"_id":{"$oid":"57###ad"},"safe":true,"document":{"date":"3\/8\/2016","description":"note-1","longitude":"72.95956905931234","latitude":"33.63043627520588"}},{"_id":{"$oid":"57###65"},"safe":true,"document":{"date":"7\/7\/2017","description":"note-2","longitude":"72.99582783132792","latitude":"33.647092700303"}}] of type org.json.JSONArray cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:158)
at org.json.JSONObject.<init>(JSONObject.java:171)
at com.example.abc.project1.MongoHQ.ReadDB.doInBackground(ReadDB.java:68)
at com.example.abc.project1.MongoHQ.ReadDB.doInBackground(ReadDB.java:32)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)

You are not correctly parsing the json string according to its structure. Your json string is an array and you are treating it as a JSONObject .

Try this -

JSONArray  jsonRootObject = null;

try {
    jsonRootObject = new JSONArray(jsonData);

    //Get the instance of JSONArray that contains JSONObjects

    //Iterate the jsonArray and print the info of JSONObjects

    for(int i=0; i < jsonRootObject.length(); i++) {

        JSONObject jsonObject = jsonRootObject.getJSONObject(i);

        JSONObject docObject = jsonObject.getJSONObject("document");
        JSONObject idObject = jsonObject.getJSONObject("_id");

        String date = docObject.optString("date").toString();
        String latitude = docObject.optString("latitude").toString();
        String longitude = docObject.optString("longitude").toString();
        String description = docObject.optString("description").toString();
        String id = idObject.optString("$oid").toString();


        result += "ROW"+i+" : \n id= "+ id + "\n date= "+ date +" \n latitude= "+ latitude+" \n longitude= "+ longitude +" \n ";
    }

    Log.d("RESULT:",result);
} catch (JSONException e) {
    e.printStackTrace();
}

If it starts with braces {} it is an object; if it starts with brackets [] it is an array. Your jsonData is not a JSON Object, it is a JSON Array, therefore it cannot be cast to a JSONObject .

Change this:

JSONObject  jsonRootObject = new JSONObject(jsonData);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("document");

To this:

JSONArray jsonArray = new JSONArray(jsonData);

And you should be good.


I noticed some more issues in the structure of your deserialization, the following for loop should work:

JSONArray jsonArray = new JSONArray(jsonData);

for(int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);

    JSONObject idObject = jsonObject.getJSONObject("_id");
    JSONObject document = jsonObject.getJSONObject("document");

    String id = idObject.optString("$oid");
    String date = document.optString("date");
    String latitude = document.optString("latitude");
    String longitude = document.optString("longitude");
    String description = document.optString("description");
    Boolean safe = jsonObject.optBoolean("safe");

    result += // your code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM