views:

94

answers:

3

Get correct data from JsonObject..

This is how the jsonstring looks like:

[
  {
    "EditLink": "http:\/\/localhost:8080\/Service.svc\/A?format=json",
    "Item": { "Value": "A" }
  },
  {
    "EditLink": "http:\/\/localhost:8080\/Service.svc\/B?format=json",
    "Item": { "Value": "B" }
  },
  {
    "EditLink": "http:\/\/localhost:8080\/Service.svc\/C?format=json",
    "Item": { "Value": "C" }
  }
]

How is it possible to get the Values only?

A: 

Hey,

If you want to get a collection of values, you can write a routine to do that:

function getItems(jsonArray) {
    var list = [];

    for (var i = 0; i < jsonArray.length; i++) {
       list.push(jsonArray[i].Item.Value);
    }

    return list;
}
Brian
jsonArray[i]-> doesn't work in java.
Troj
Sorry, was thinking JavaScript...
Brian
A: 

You might want to check out this post -- some good links to JSON Java libraries:

http://stackoverflow.com/questions/338586/a-better-java-json-library

Nicholas Cloud
Iam jusing jSonObject, from import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject; what is wrong with that?
Troj
Um. Nothing is wrong with that. I thought you were asking for the means to get values from a JSON string, so I posted a link to a similar post.
Nicholas Cloud
I would actually suggest having a look at alternatives, most have more convenient/powerful accessors, which would help here.
StaxMan
A: 

Using Jackson 1.6, you could get values like so:

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
List<JsonNode> values = root.findValues("Value");
// or if you want values as String, use "findValuesAsText" to get List<String>

and you can access specific type (number, boolean, etc) via JsonNode accessors.

StaxMan