gpt4 book ai didi

jquery - 从 jquery 中的 json 中提取部分

转载 作者:行者123 更新时间:2023-12-01 07:02:34 25 4
gpt4 key购买 nike

我有一个 json 字符串,需要提取其中的某些部分并将其发送到服务。我想知道是否可以使用表达式或任何其他方法提取 jquery 中的特定部分?

我的示例 json 是

{"hashcode":[], "stringMap":{":id":"50",":question":"My roof"}, ":size":"2", ":type":"java.util.HashMap", ":typeId":"123"}

我想要的字符串是

{":id":"50",":question":"My roof"}

提前致谢。

感谢大家的投入。我刚刚意识到 stringMap 可能有一个任意名称,例如 stringMap_0 等......我仍然可以在不确切知道该名称的情况下取出该部分吗?

再次感谢。

最佳答案

让我们明确一点,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”之前的“:”...

关于jquery - 从 jquery 中的 json 中提取部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1794932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com