1

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);

4
  • 2
    You seem to be confusing Java and JavaScript. They are entirely different. Please review your question and tag it appropriately. Commented Aug 12, 2013 at 10:57
  • @rory-mccrossan no no, I built a web service using java, but the return value is JSON and I should handle it with js Commented Aug 12, 2013 at 10:59
  • @HasanKhatib We'll need to know how you're passing this to JavaScript. Commented Aug 12, 2013 at 11:00
  • @JamesMcLaughlin I used an ajax request to get the json from this service that I built, now I want to fill the html with this json Commented Aug 12, 2013 at 11:05

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.