Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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数组_Android_Json_Parsing - Fatal编程技术网

Android解析多个JSON数组

Android解析多个JSON数组,android,json,parsing,Android,Json,Parsing,我试图解析服务器返回到应用程序的多个JSON数组 从服务器返回的消息的结构如下所示: [json对象][json对象] 返回消息: [{“纬度”:“44.33”,“经度”:“44.33”,“名称”:“test1”,“通知”:“true”}][{“纬度”:“44.33”,“经度”:“44.33”,“名称”:“test2”,“通知”:“false”}] 现在我试图解析它并获得通知状态(true/false),但没有得到所需的项目 try { JSONArray jr = new

我试图解析服务器返回到应用程序的多个JSON数组

从服务器返回的消息的结构如下所示:

[json对象][json对象]

返回消息:

[{“纬度”:“44.33”,“经度”:“44.33”,“名称”:“test1”,“通知”:“true”}][{“纬度”:“44.33”,“经度”:“44.33”,“名称”:“test2”,“通知”:“false”}]

现在我试图解析它并获得通知状态(true/false),但没有得到所需的项目

  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();
  }

如果有人能帮助我理解我需要做什么来完成我的任务,我会很高兴。

Json响应不能作为一个整体来解析。从形式上讲,它不是json有效的,有两个json数组像这样连接在一起,并不构成有效的json

您可以将这些数组包装在一个唯一的json对象中,该对象现在可以与您的代码进行解析

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

然后,您可以实例化jsonObject,并分别使用名称获取每个数组,因为Json响应不能作为一个整体进行解析。从形式上讲,它不是json有效的,有两个json数组像这样连接在一起,并不构成有效的json

您可以将这些数组包装在一个唯一的json对象中,该对象现在可以与您的代码进行解析

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

然后,您可以实例化jsonObject,并分别使用每个数组的名称获取它们的Json响应无效

这是有效的JSON:

  [
    [{
        "latitude": "44.33",
        "longitude": "44.33",
        "name": "test1",
        "Notification": "true"
    }],
    [{
        "latitude": "44.33",
        "longitude": "44.33",
        "name": "test2",
        "Notification": "false"
    }]
  ]
要分析它,请执行以下操作:

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();
}
试试看{
JSONArray json=新JSONArray(答案);

对于(inti=0;i您的Json响应无效

这是有效的JSON:

  [
    [{
        "latitude": "44.33",
        "longitude": "44.33",
        "name": "test1",
        "Notification": "true"
    }],
    [{
        "latitude": "44.33",
        "longitude": "44.33",
        "name": "test2",
        "Notification": "false"
    }]
  ]
要分析它,请执行以下操作:

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();
}
试试看{
JSONArray json=新JSONArray(答案);

对于(inti=0;i创建响应的POJO,比如Notification.java

JSONArray resultArray = response.getJSONArray("result");// your json response 
//谷歌使用Gson库

编译'com.google.code.gson:gson:2.7.0'

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

创建一个POJO响应,比如Notification.java

JSONArray resultArray = response.getJSONArray("result");// your json response 
//谷歌使用Gson库

编译'com.google.code.gson:gson:2.7.0'

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


您可以使用GSON进行解析,这要简单得多。使用GsonFormatter Android studio插件创建模型类您的第一行回答是什么?您是否收到任何错误?是的,我收到下一个错误-“java.lang.String类型通知时的值true无法转换为JSONArray”您的返回消息不是有效的JSON。请尝试用一个键将这些数组包装到一个数组中。如下所示:您应该使用
String nof=jb.getString(“通知”);
而不是
JSONArray nof=jb.getJSONArray(“通知”);
来打印,只需打印nof的值,如
Log.d(“JSON”,nof);
您可以使用GSON进行解析,这要简单得多。使用GsonFormatter Android studio插件来创建模型类您的第一行中的答案是什么?您是否收到任何错误?是的,我收到了下一个错误-“java.lang.String类型通知时的值true无法转换为JSONArray”您的返回消息不是有效的JSON。请尝试用一个键将这些数组包装到一个数组中。如下所示:您应该使用
String nof=jb.getString(“通知”);
而不是
JSONArray nof=jb.getJSONArray(“通知”);
来打印,只需打印nof的值,如
Log.d(“JSON”,nof)
他在尝试获取
通知的值时出错,如果是无效的json,则异常应该是其他异常。我认为发帖提问是错误的,请参阅问题中的注释,我为什么说…这不是有效的JSONI尝试一下,但在“JSONArray array=json.getJSONArray(i);“我得到错误-“类型为org.json.JSONObject的0处的值无法转换为JSONArray”在您的响应中,您得到的是,,一个json对象…发布完整的响应…您发布的问题响应是错误的!问题是当我检查他是1时数组的大小,它需要是2(json.length())因此,在您的代码中,只获取第一个元素才是问题所在。我如何获取数组中的所有元素?他在尝试获取
通知的值时出错,如果它是无效的json,则异常应该是其他情况。我认为发布问题是错误的,请参阅问题中的注释,我为什么说……这不是问题有效的JSONI请尝试此操作,但在“JSONArray array=json.getJSONArray(i);”行中,“我得到错误-”类型为org.json.JSONObject的0处的值无法转换为JSONArray“好的,在您的响应中,您得到了,,一个JSON对象…发布完整的响应…您在问题上发布的响应是错误的!问题是当我检查他是否为1时,数组的大小必须为2(JSON.length())因此,在您的代码中,只获取第一个元素,这就是问题所在。我如何获取数组的所有元素?谢谢,但如何将正确的返回字符串转换为您所回答的类型?这取决于您在服务器端生成JSonTanks时使用的语言,但如何将正确的返回字符串转换为类型就像您回答的那样?这取决于您在服务器端生成json所使用的语言