2

sorry but i amm not a player of Json it's my first time. so please tell me how can i parse a the following rsponse using jsonArray. As JsonObject need an string to return the the JsonArray but in my case i have dynamic Keys as integer and i have to parse it as it is....

{
"1":{
    "services":{
        "2":{
            "service":{
                "title":"test 1 service",
                "defaultDuration":"60",
                "brief":"testing brief ...",
                "classSize":"6",

            }
        },
        "3":{
            "service":{
                "title":"test service 2",
                "defaultDuration":"60",
                "brief":"testing 2 brief... ",
                "classSize":"1",
            }
        },
        "organisation":{
        "title":"First organization",
        "url":"http:\/\/test.com\/main",
        "address":"First Floor ",
        "suburb":"Arma",
        "postcode":"31435",
        "state":"VIC",
        "country":"AU"
    }
},
"643":{
    "services":{
        "1325":{
            "service":{
                "title":"test 1 service",
                "defaultDuration":"90",
                "brief":"test 1 sevice brief,
                "classSize":"1",
            }
        },
        "1523":{
            "service":{
                "title":"test 2 service",
                "defaultDuration":"90",
                "brief":"test 2 brief.",
                "classSize":"1",
            }
        },
        "organisation":{
        "title":"Second orginization",
        "url":"http:\/\/test.com\/main2",
        "address":"18 Street",
        "suburb":"South ",
        "postcode":"1241",
        "state":"Teststate",
        "country":"US"
    }
}

}

2
  • I would just parse it normally, then use the 'for ( i in o )' loops to get the data out, and put it in an array. This way, you don't need to write a custom parse, just custom logic. Much easier, I would think. Commented Aug 13, 2011 at 23:25
  • i parse it using json object but after that iteration the positions are not as it. see my first question stackoverflow.com/questions/7051785/… Commented Aug 13, 2011 at 23:27

1 Answer 1

2

Your real problem is that the JSON you are parsing is NOT an array. It is an object (a dictionary) whose keys are numbers.

If you really need it to be an array, you will need to parse it as a JsonObject and build the array yourself by iterating over the object's keys, etcetera.


I would also add that this is not a good idea, if the sample you have provided is indicative. The resulting array will be sparse; i.e. there will be lots of "holes" where there is no entry for a given index. This could waste a lot memory ... depending on the actual keys.

Sign up to request clarification or add additional context in comments.

2 Comments

then how can i parse it. becoz using iterator it keys are changed not in order as in string.
Parse it as an object, then iterate through the keys, transform each key into an Integer, and place each key with its associated value in a TreeMap. The keys will then be sorted in ascending order.

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.