Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4604278
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:16:21+00:00 2026-05-22T00:16:21+00:00

I have done JSON parsing earlier but this one is not working for me.

  • 0

I have done JSON parsing earlier but this one is not working for me. I don’t know why. It’s a quite simple 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
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:16:21+00:00Added an answer on May 22, 2026 at 12:16 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have done JSON parsing without using async task, it is working fine in
I am using google Api for custom search and have done the json Parsing.
Have done quite a bit of searching for a guide (of any substance) for
I have done one program that allow user can listen to the music via
I have done some looking and I found this: http://www.codeproject.com/Articles/14439/The-ScrollableListBox-Custom-Control-for-ASP-NET-2 but to me it
I am retrieving a string of values from json url. I have done it
I've got this parsing operation that currently works fine, but I've started to notice
I want to pass a JSON data structure to an MVC (3) Controller, have
Why JSON? I have done some tests today, and the request time for both
Receiving and sending data with JSON is done with simple HTTP requests. Whereas in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.