0

i am converting a xml content into JSON content and the content is as follows

{
    "response":
    {
        "seatlist":
        {
            "seat":
            {
                "balance":85694.6,"num":12
            },
            "seat":
            {
                "balance":85694.6,"num":12
            }
        },
        "userid":"8970ca285d9c4e4d",
        "seatnum":12,
        "session":"online"
    }
}

I am able to get the userid and seatnum in the following way

JSONObject response = json.getJSONObject("response");
            out.setUserid(response.getString("userid"));
            out.setBalance(Double.valueOf(response.getString("balance")));

Now the problem is i need to parse the following content and need to get "num" value

"seatlist":
        {
            "seat":
            {
                "balance":85694.6,"num":12
            },
            "seat":
            {
                "balance":85694.6,"num":12
            }
        }

here is my code i am using

JSONObject objList = response.getJSONObject("seatlist");
String n = String.valueOf(objList.getJSONObject("seat"));
if(objList.getJSONObject("seat").get("userId").equals(userId)) { String num = String.valueOf(objList.getJSONObject("seat").get("num")); out.setSeatNum(Integer.valueOf(num)); }

if i have one seat value i am able to get "num" value else i am getting JSON exception

Pls give me a suggestion in this.....

2 Answers 2

2

You want to get a JSONArray in an JSONObject. That is not possible. Get the Array and then iterate through the array to get the objects:

JSONObject objList = response.getJSONArray("seatlist");
for(int i = 0, i<objList.lenght(); i++){
   JSONObject json = objList.get(i);
}
Sign up to request clarification or add additional context in comments.

Comments

1
{
"response":
{
    "seatlist":
    {
        "seat":
        {
            "balance":85694.6,"num":12
        },
        "seat":
        {
            "balance":85694.6,"num":12
        }
    },
    "userid":"8970ca285d9c4e4d",
    "seatnum":12,
    "session":"online"
}
}

means an object with an object called response. this contains an object called seatlist. this contains 2 objects called seat (but this is wrong! this should be an array!). and so on..

to read this you can use

JSONObject json = new JSONObject(yourresponse);
JSONObject response = json.getJSONObject("response");

JSONObject seatlist = response.getJSONObject("seatlist");
JSONObject userid = response.getJSONObject("userid");
JSONObject seatnum = response.getJSONObject("seatnum");
JSONObject session = response.getJSONObject("session");

now seatlist contains.

   {
    "seat":
    {
        "balance":85694.6,"num":12
    },
    "seat":
    {
        "balance":85694.6,"num":12
    }
}

which is wrong, since it contains 2 elements with same name. Now you can either call it by index like this:

 JSONObject seat1 = seatlist.getJSONObject(1);
 JSONObject seat2 = seatlist.getJSONObject(2);


 seat1.getString("balanace"); seat1.getInt("num");

or you can iterate through the JSONObject.

At least it should be an Array instead of JSONOBject.

This means it should look like this.

{
"response": {
"seatlist": [
    "seat":
    {
        "balance":85694.6,"num":12
    },
    "seat":
    {
        "balance":85694.6,"num":12
    }
],
"userid":"8970ca285d9c4e4d",
"seatnum":12,
"session":"online"
} 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.