Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following JSON data structure:

{
    "myRequest": {
    "item1": "value1",
    "itme2": "value2",
    "item3Holder": {
        "id": [
        "ID001",
        "ID002",
        "ID003",
        "ID004"
        ]
    }
    }
}       

I need to be able to get the id values from the id [] array.

I can can get the values for item1-3, but can't separate out the id [] array values.

JSONObject requestObj = new JSONObject(data.trim()).getJSONObject("myRequest");

// Retrieve items from JSONObject
String item1 = requestObj.getString("item1");
String item2 = requestObj.getString("item2");
String item3 = requestObj.getString("item3");

// Retrieve all id's
JSONArray ids = requestSubObj.getJSONArray("item3Holder");

for (int i = 0; i < ids.length(); i++) {
    String id = ids.toString();
    logger.info("id : " + id);
}
share|improve this question
 
What is requestSubObj? –  Simon André Forsberg Sep 25 '12 at 13:17
add comment

1 Answer

up vote 1 down vote accepted

I think you should get the objects like this:

JSONObject item3Holder = requestObj.getJSONObject("item3Holder");
JSONArray ids = item3Holder.getJSONArray("ids");

Also you need to call .getString(i) for the ids object

for (int i = 0; i < ids.length(); i++) {
    String id = ids.getString(i).toString();
    logger.info("id : " + id);
}
share|improve this answer
 
that worked great thanks very much –  babb Sep 25 '12 at 13:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.