Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.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,我正在分析android应用程序中的一些JSON,这是我开始使用的代码: JSONObject jsonObject = **new JSONObject(result);** int receivedCount = **jsonObject.getInt("CurrentCount");** 然而,这导致它出错(出错的代码被星号包围)在Android Studio中,我尝试使用建议功能,该功能询问我是否需要“try/catch包围”,这将导致应用程序在启动时崩溃 这是建议的代码: JSONO

我正在分析android应用程序中的一些JSON,这是我开始使用的代码:

JSONObject jsonObject = **new JSONObject(result);**
int receivedCount = **jsonObject.getInt("CurrentCount");**
然而,这导致它出错(出错的代码被星号包围)在Android Studio中,我尝试使用建议功能,该功能询问我是否需要“try/catch包围”,这将导致应用程序在启动时崩溃

这是建议的代码:

JSONObject jsonObject = null;
try {
    jsonObject = new JSONObject(result);
} catch (JSONException e) {
    e.printStackTrace();
}

int receivedCount = 0;
try {
    receivedCount = jsonObject.getInt("CurrentCount");
} catch (JSONException e) {
    e.printStackTrace();
}
这是我试图传递的JSON:

[{"CurrentCount":"5"},{"CurrentCount":"0"},{"CurrentCount":"1002"}]

提前谢谢

J_Dadh我认为首先,您应该查看有关如何使用Json解析器的文档,您可以在下面的链接中找到这些文档https://www.tutorialspoint.com/android/android_json_parser.htm

示例JSON
{
“系统”:
{
“国家”:“GB”,
“日出”:1381107633,
“日落”:1381149604
},
“天气”:[
{
“id”:711,
“主要”:“烟雾”,
“说明”:“烟雾”,
“图标”:“50n”
}
],
“主要”:
{
“温度”:304.15,
“压力”:1009,
}
}
字符串YourJsonObject=“JsonString”
JSONObject JSONObject=新的JSONObject(您的JSONObject);
JSONArray weather=jsonObj.getJSONArray(“天气”);
对于(int i=0;i
粘贴JSON时,您正在使用
[{“CurrentCount”:“5”}、{“CurrentCount”:“0”}、{“CurrentCount”:“1002”}]

-如果我们分析这个JSON,这个JSON包含一个JSON数组
[{“CurrentCount”:“5”},{“CurrentCount”:“0”},{“CurrentCount”:“1002”}]
有3个JSON对象
{CurrentCount:“5”},{“CurrentCount:“0”},{“CurrentCount:“1002”}

-但是当您要解析这个JSON时,您接受它为
jsonObject=new-jsonObject(result)
,但您应该接受它为
JSONArray-JSONArray=new-JSONArray(result)

然后在这个
jsonArray
上迭代一个循环(例如for循环),接受JSONObject 1乘1,然后从每个JSONObject 1乘1获得值

-JSON中的另一个错误是,您将字符串作为“5”发送,但将其接受为
getInt()
这是不公平的,您应该发送
int
将其接受为
int
作为5(没有双qoutes)

因此,您需要最终的JSON和如下代码(如下所示) JSON
[{“CurrentCount”:5},{“CurrentCount”:0},{“CurrentCount”:1002}]

使用此JSON的代码

JSONOArray jsonArray = null;
try{
       jsonArray = new JSONArray(result);
       for(int i=0;i<jsonArray.length;i++){
       JSONObject jsonObject=jsonArray[i];
       receivedCount=jsonObject.getInt("CurrentCount");
       }
   }catch(JSONException e) {
       e.printStackTrace();
   }
jsonArray jsonArray=null;
试一试{
jsonArray=新jsonArray(结果);

对于(int i=0;我不确定…您的JSON不是JSONObject。这是JSONArray。可能与
JSONOArray jsonArray = null;
try{
       jsonArray = new JSONArray(result);
       for(int i=0;i<jsonArray.length;i++){
       JSONObject jsonObject=jsonArray[i];
       receivedCount=jsonObject.getInt("CurrentCount");
       }
   }catch(JSONException e) {
       e.printStackTrace();
   }