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

My Json is:

{
"Response":{
"Asset":[
{
"id":2461,
"name":"TestAsset7771",
"model_name":"TestModel777",
"serial_number":"TestAsset7771",
"current_data":{
"timestamp":"",
"name":"Temperature",
"value":"?"
}
},
{
"id":2448,
"model_id":1229,
"name":"TestAsset777",
"model_name":"TestModel777",
"serial_number":"TestAsset777",
"current_data":{
"timestamp":"",
"name":"Temperature",
"value":"?"
}
}
]
}
}

My code is:

 JSONObject outerObject = new JSONObject(jsonObj.toString());
JSONObject innerObject = outerObject.getJSONObject("Response");
JSONArray jsonArray = innerObject.getJSONArray("Asset");
for (int i = 0, size = jsonArray.length(); i < size; i++)
{
  JSONObject objectInArray = jsonArray.getJSONObject(i);
  String[] elementNames = JSONObject.getNames(objectInArray)
  for (String elementName : elementNames)
  {
    String value = objectInArray.getString(elementName);
    System.out.printf("name=%s, value=%s\n", elementName, value);
  }
}

For inner array - ie current data, am getting values as:

name=current_data, value={"timestamp":"","name":"Temperature","value":"?"}

How can i put another inner array so that i can get values of "timestamp":"", "name":"Temperature", "value":"?" in separate variables instead of complete JSON

share|improve this question
    
Uh, "value" is a map. Extract the entries from the map. –  Hot Licks Oct 30 '13 at 15:23
    
value is json again.. –  user2572739 Oct 30 '13 at 15:26
    
If the above JSON listing is accurate and you used a plain JSON parser, the value of "current_data" will be a Map. For the particular JSON parser you're using the form of Map would be a JSONObject. Access the elements just as you'd access any other Map. –  Hot Licks Oct 30 '13 at 15:57
add comment

4 Answers

Its better to use Gson to parse JSON. Anyway, if you decide to follow as this is, try as :

You have a class like this:

class CurrentData{
  String name,timestamp,value;
  void print(){
    System.out.printf("name=%s, timestamp=%s, value=%s\n", name,timestamp, value);
  }
}

Now, change your for loop as follows:

 for (String elementName : elementNames)
  {
    if(!elementName.equals("current_data")){
    String value = objectInArray.getString(elementName);
    System.out.printf("name=%s, value=%s\n", elementName, value);
    }
    else{
    CurrentData obj=new CurrentData();// You can use array of objects declaring outside the loop as your need
    JSONObject curr_object=objectInArray.getJSONObject("current_data");
    obj.name=curr_object.getString("name");
    obj.timestamp=curr_object.getString("timestamp");
    obj.value=curr_object.getString("value");
    obj.print();
    }
  }
share|improve this answer
    
using GSON to parse is a good idea.. let me try that once.. –  user2572739 Oct 30 '13 at 15:54
    
That's good. Happy coding :) –  Nizam Oct 30 '13 at 15:57
    
Why iterate through elementNames vs just accessing objectInArray.getJSONObject("current_data")?? –  Hot Licks Oct 30 '13 at 16:01
    
@HotLicks He/She wrote iteration, not for "current_data", but for other elements. Just look for "model_id" for example. –  Nizam Oct 30 '13 at 16:09
    
Except why iterate on Map elements? You presumably want specific values and no sense going through values you don't want. –  Hot Licks Oct 30 '13 at 16:54
show 3 more comments
for (String elementName : elementNames)
  {
  JSONObject jsonobject = jsonarray.getJSONObject(elementName);

    System.out.printf( "name=%s, value=%s\n",jsonobject.getString("name"),jsonobject.getString("value"));

  }
share|improve this answer
    
You're not accessing "current_data" first. –  Hot Licks Oct 30 '13 at 16:08
    
@HotLicks thanks you are right. –  Nambi Narayanan Oct 30 '13 at 16:11
add comment

"value" is another jason object, so you can just call "getJasonObject()" to obtain the item and then proceed with that new array as normal.

Edit: I made a fail (not enough C0FFEE in my memory) and corrected thanks to the comment.

share|improve this answer
    
Actually it's a JSON object -> JSONObject -> Map. –  Hot Licks Oct 30 '13 at 15:58
    
Yes, I find that COFFEE has a very narrow value range -- easy to underflow and overflow. –  Hot Licks Oct 30 '13 at 16:56
add comment
JSONObject outerObject = new JSONObject(jsonObj.toString());
JSONObject innerObject = outerObject.getJSONObject("Response");
JSONArray jsonArray = innerObject.getJSONArray("Asset");
for (int i = 0, size = jsonArray.length(); i < size; i++) {
  JSONObject objectInArray = jsonArray.getJSONObject(i);
  JSONObject currentData = objectInArray.getJSONObject("current_data");
  if (currentData != null) {
      String timestamp = currentData.getString("timestamp");
      String name = currentData.getString("name");
      String value = currentData.getString("value");
      // Assign above results to array elements or whatever
  }

}

share|improve this answer
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.