Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中解析json数据时出现org.json.JSONException_Android_Json - Fatal编程技术网

Android中解析json数据时出现org.json.JSONException

Android中解析json数据时出现org.json.JSONException,android,json,Android,Json,在androidstudio中,我试图在我的android应用程序中解析JSON数据。为此,我编写了这段代码(如下所示)。但是这段代码抛出了org.json.JSONException。实际上,我想要这样的输出: /*This is just a sample*/ ROW: 1 id = ### date = 0/0/0 latitude = 12.123456789 longitude = 12.123456789 description = any note 我的代码: String js

在androidstudio中,我试图在我的android应用程序中解析JSON数据。为此,我编写了这段代码(如下所示)。但是这段代码抛出了
org.json.JSONException
。实际上,我想要这样的输出:

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

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);
String jsonData=“[”+
“{U id:{oid:{57#######ad”},文件:{“日期”:“2016年3月8日”,“纬度”:“33.63043627520588”,“经度”:“72.95956905931234”,“说明”:“注-1”},“安全”:真},”+
“{U id:{oid:”57########65“}”,文件:{“日期:”2017年7月7日,“纬度:”33.647092700303,“经度:”:“72.99582783132792”,“说明:”:“注2”},“安全”:真}”+
"]";
JSONObject jsonRootObject=新的JSONObject(jsonData);
//获取包含JSONObject的JSONArray实例
JSONArray JSONArray=jsonRootObject.optJSONArray(“文档”);
//迭代jsonArray并打印JSONObject的信息
for(int i=0;i
错误

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)
05-04 23:56:48.475 9762-11393/com.example.abc.project1 E/错误:异常
org.json.json.org.json.json.json.org.json.json.org.json.json.org.json.json.org.json.json.json.json.json.json.json.org.json.org.json.org.json.org.json.org.org.org.org.gov.35周五周五周五除了除了除了除了除了“安全”:真实,文件::{日期:“3\/8/8\/8\/2016年8\/2016/2016年,描述描述,描述描述描述:注-1,描述描述描述:注注-1,经线,经度:注-1,经度:注1,经度:注注1,经度:注1,经度:注1,经度:72.1,经度:72.1,经度:72.1,经度,经度:72.95959595959595959595959595959595955959}]无法将org.json.JSONArray类型的转换为JSONObject
在org.json.json.typeMismatch(json.java:111)
位于org.json.JSONObject(JSONObject.java:158)
位于org.json.JSONObject(JSONObject.java:171)
位于com.example.abc.project1.MongoHQ.ReadDB.doInBackground(ReadDB.java:68)
位于com.example.abc.project1.MongoHQ.ReadDB.doInBackground(ReadDB.java:32)
在android.os.AsyncTask$2.call(AsyncTask.java:287)
在java.util.concurrent.FutureTask.run(FutureTask.java:234)处
在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
运行(Thread.java:838)

您没有根据json字符串的结构正确解析该字符串。 您的json字符串是一个数组,您将其视为一个
JSONObject

试试这个-

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();
}
JSONArray-jsonRootObject=null;
试一试{
jsonRootObject=新的JSONArray(jsonData);
//获取包含JSONObject的JSONArray实例
//迭代jsonArray并打印JSONObject的信息
for(int i=0;i
您没有根据json字符串的结构正确解析该字符串。 您的json字符串是一个数组,您将其视为一个
JSONObject

试试这个-

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();
}
JSONArray-jsonRootObject=null;
试一试{
jsonRootObject=新的JSONArray(jsonData);
//获取包含JSONObject的JSONArray实例
//迭代jsonArray并打印JSONObject的信息
for(int i=0;i
如果它以大括号开始,则它是一个对象;如果它以大括号开始,则它是一个数组。您的
jsonData
不是一个JSON对象,它是一个JSON数组,因此不能将它强制转换为
JSONObject

更改此项:

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

JSONArray jsonArray = new JSONArray(jsonData);
你应该表现得很好


我注意到反序列化结构中还有一些问题,下面的for循环应该可以工作:

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
}
JSONArray-JSONArray=新的JSONArray(jsonData);
for(int i=0;i