让我们在这里明确一点,JSON 只是 Javascript 对象表示法。所以你有几个对象:
{"hashcode":[], "stringMap":{":id":"50",":question":"My roof"}, ":size":"2", ":type":"java.util.HashMap", ":typeId":"123"}
进一步分解这段代码,我们发现如下:
"hashcode":[] //an empty array named hashcode
"stringMap":{":id":"50",":question":"My roof"} //an object named stringMap with two properties, ":id" and ":question" (not sure why the : are there, this is abnormal)
":size":"2"//a string ":size" set to the string "2" (again, what's with the :?)
":type":"java.util.HashMap"//a string ":type" set to the string "java.util.HashMap"
":typeId":"123"//a string ":typeId" set to the string "123"
您通常可以在 Javascript 中使用“点”表示法来引用这些对象中的任何一个。整个事情的功能很像Java Enum/Hashmap/ArrayList。但是,由于那些“:”,您在整个过程中将无法正常工作。不过通常情况下,您可以执行以下操作 (see POC here):
<script type="text/javascript">
var jsonString = '{"hashcode":[], "stringMap":{"id":"50","question":"My roof"}, "size":"2", "type":"java.util.HashMap", "typeId":"123"}';
var data = eval("(" + jsonString + ")");
alert(data.stringMap.id);
</script>
请注意,为了使该代码正常工作,我必须从“id”之前删除“:”...