Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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解析在Java中未按预期工作_Android_Json_Parsing - Fatal编程技术网

Android JSON解析在Java中未按预期工作

Android JSON解析在Java中未按预期工作,android,json,parsing,Android,Json,Parsing,我之前已经做过JSON解析,但是这个对我来说不起作用。我不知道为什么。这是一个相当简单的结构,但我仍然无法修复它 下面是我从我的URL得到的回复 { "BalanceItems" : [{ "BalanceItem" : { "BalanceType" : "General Cash", "Description" : "Prepay Credit", "ShortDe

我之前已经做过JSON解析,但是这个对我来说不起作用。我不知道为什么。这是一个相当简单的结构,但我仍然无法修复它

下面是我从我的URL得到的回复

{
    "BalanceItems" : [{
            "BalanceItem" : {
                "BalanceType" : "General Cash",
                "Description" : "Prepay Credit",
                "ShortDescription" : "Your bal is $",
                "Value" : 187.95,
                "Unit" : "$NZ",
                "ExpiryValue" : "",
                "ExpiryDate" : "18\/02\/2012"
            }
        }, {
            "BalanceItem" : {
                "BalanceType" : "Me 2 U",
                "Description" : "Me2U Credit",
                "ShortDescription" : "Your bal is $",
                "Value" : 176.86,
                "Unit" : "$NZ",
                "ExpiryValue" : "176.86",
                "ExpiryDate" : "12\/06\/2011"
            }
        }, {
            "BalanceItem" : {
                "BalanceType" : "Rate Plan Recharge Reward",
                "Description" : "Your 'top up and get' special rates",
                "ShortDescription" : "Your 'top up and get' rates are yours until",
                "Value" : ""
            }
        }
    ]
}
这是我的密码

try{

            URL url = new URL("myurl");         
            URLConnection connection = url.openConnection();   

            String line;   
            StringBuilder builder = new StringBuilder();   
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

           while((line = reader.readLine()) != null) {    
               builder.append(line);   
           }   

           String response = builder.toString();   

           JSONObject jar = new JSONObject(response);
           Log.e("Object Size","Object Size  "+ jar.length());
           // Here i should get the size of object as 3 because i have three object of type "BalanceItem"

        }
        catch (Exception e) {
            // TODO: handle exception
        }

您有一个包含JSONArray的JSON对象。您需要获取JSONArray:

JSONArray array = jar.getJSONArray("BalanceItems");
Log.e("Object Size","array Size  "+ array.length());
请在此处查看API:


附录:

数组依次包含几个JSONObject。您需要对其进行迭代并获得每一个:

for (int i=0; i<array.length(); i++) {
  JSONObject obj = array.getJSONObject(i);
  Log.d ("Balance "+i, obj.getString("Value") + " " + obj.getString("Unit")); 
}

for(int i=0;i您有一个包含JSONArray的JSON对象。您需要获取JSONArray:

JSONArray array = jar.getJSONArray("BalanceItems");
Log.e("Object Size","array Size  "+ array.length());
请在此处查看API:


附录:

该数组依次包含多个JSONObject。您需要对其进行迭代并获取每个JSONObject:

for (int i=0; i<array.length(); i++) {
  JSONObject obj = array.getJSONObject(i);
  Log.d ("Balance "+i, obj.getString("Value") + " " + obj.getString("Unit")); 
}


for(int i=0;i打开你的日志、错误、代码,然后人们会试图帮助你。你解析JSON的代码是什么样子的?实际上,
jar.length()
是1。它只包含
BalanceItems
数组。JSONArray BalanceItems长度应该是3。如果我没有弄错,BalanceItems是数组,BalanceItem是对象。那么我如何获取BalanceItem对象并获取其值。Sujit,请用您的代码编辑您的问题。发布您的日志、错误、代码,然后人们会尝试为了帮助您,您解析JSON的代码是什么样子的?实际上,
jar.length()
是1。它只包含
BalanceItems
数组。JSONArray BalanceItems长度应该是3。如果我没有错的话,BalanceItems是数组,BalanceItem是对象。那么我如何获取BalanceItem对象及其值。Sujit,请用您的代码编辑您的问题。嗨,Aleadam,谢谢您的回答,但我如何获取所有的值BalanceItem中的值,如BalanceType、Description等。每个BalanceItem本身都是一个JSON对象。我会在回答中添加更多细节。请帮助我帮助您:描述它是如何工作的,以便我了解需要修复的内容。首先,告诉我使用
jar.toString()可以得到什么
Aleadam。我已经在你的代码下面添加了我的代码,还添加了我正在使用的URL。我需要解析所有的值。我不知道它为什么不起作用……告诉我
Log.d(“test”,response);
(String response=builder.toString();)和
Log.d(“test2”,jar.toString());
(在JSONObject jar=newJSONObject(响应)之后)嗨,Aleadam,谢谢你的回复,但我如何才能获得BalanceItem中的所有值,如BalanceType、Description等。每个BalanceItem本身就是一个JSON对象。我将在回答中添加更多细节。请帮助我帮助你:描述它是如何工作的,以便我了解需要修复的内容。首先,告诉我
j有什么好处ar.toString()
Aleadam。我已经在你的代码下面添加了我的代码,还添加了我正在使用的URL。我需要解析所有的值。我不知道它为什么不起作用……告诉我
Log.d(“test”,response);
(字符串response=builder.toString();)和
Log.d(“test2”,jar.toString());
(在JSONObject jar=newJSONObject(响应)之后)