简体   繁体   English

检索js中嵌套对象的JSON

[英]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. 我已经使用Java构建了一个Web服务,并且返回值是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. 问题是我有一个对象,其中包含一个Hashmap<>作为参数,当我将其解析为JSON并将其返回时,如何在js中处理它,如何获取哈希图的值。

Here is the object that I parse to JSON. 这是我解析为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); 返回aGson.toJson(human);

Once you've received your JSON text from the web service, parse it in JavaScript as 从Web服务接收JSON文本后,请在JavaScript中将其解析为

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

Use the org.json.JSONObject class, like this: 使用org.json.JSONObject类,如下所示:

JSONObject jsonHuman = new JSONObject( human );

It should use reflection to find all the public fields and build a valid JSON object for you. 它应该使用反射来查找所有公共字段并为您构建一个有效的JSON对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM