开发者

JSON parsing not working as expected in Java

开发者 https://www.devze.com 2023-03-06 00:52 出处:网络
I have done JSON parsing earlier but this one is not working for me. I don\'t know why. It\'s a quite simpl开发者_运维问答e structure still I am not able to fix it.

I have done JSON parsing earlier but this one is not working for me. I don't know why. It's a quite simpl开发者_运维问答e structure still I am not able to fix it.

Here is the response that I am getting from my 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" : ""
            }
        }
    ]
}

Here is my code

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
        }


You have a JSON object that contains a JSONArray. You need to get the JSONArray:

JSONArray array = jar.getJSONArray("BalanceItems");
Log.e("Object Size","array Size  "+ array.length());

Check the API here:

http://www.json.org/javadoc/org/json/JSONObject.html#getJSONArray%28java.lang.String%29

http://www.json.org/javadoc/org/json/JSONArray.html


Addendum:

The array in turn contains several JSONObjects. You need to iterate through it and get each one:

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

You get the idea. Typed in here on top of my head, so beware of the typos.

0

精彩评论

暂无评论...
验证码 换一张
取 消