hasan.alkhatib
hasan.alkhatib

Reputation: 1559

retrieve the JSON of nested objects in js

I have built a web service using java, and the return value is a parsed object to JSON.

the problem is that I have an object that contains a Hashmap<> in it as a parameter, when I parse it to JSON and returns it, How could I handle it in js, how could I get the values of the hashmap.

Here is the object that I parse to JSON.

Object human;

Hashmap<String, String> properties;

properties.put("property1", "value");
properties.put("property2", "value");
properties.put("property3", "value");

/* here where I got the object that contains several attributes  beside the hashmap that is considered as object*/

human.setProperties(properties);

return aGson.toJson(human);

Upvotes: 1

Views: 203

Answers (2)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Once you've received your JSON text from the web service, parse it in JavaScript as

var human = JSON.parse( jsonTextFromWS );
console.log( human.properties.property1 ); // value

Upvotes: 1

user2674598
user2674598

Reputation: 11

Use the org.json.JSONObject class, like this:

JSONObject jsonHuman = new JSONObject( human );

It should use reflection to find all the public fields and build a valid JSON object for you.

http://www.json.org/javadoc/org/json/JSONObject.html

Upvotes: 0

Related Questions