JSON 字符串中出现意外的空键值
我正在尝试解析嵌入在我的 html 文件中的 json 字符串。 这是简化的代码。
<html>
<head>
<script src="./jquery-1.4.4.min.js" type="text/javascript"></script>
<script>
function parse_json(){
var jtext = $("#mtxt").text();
var jdata = jQuery.parseJSON(jtext);
JSON.parse(JSON.stringify(jdata), function (key, value){
alert("key=" + key + " value=" + value);
if(key== ""){
alert("value in string" + JSON.stringify(value));
}
});
}
$(document).ready(function() {
$("#run").click( function () {
parse_json();
});
});
</script>
</head>
<body>
<a id="run" href="#">run</a>
<div id="mtxt">
{"caller": "539293493"}
</div>
</body>
</html>
当我解析它时,除了预期的“调用者”值之外,我还得到一个额外的空“键”和“值”。 第一个警报告诉我
key= value=[object Object]
第二个警报告诉我
value in string{}
发生了什么?为什么要添加这个额外条目?
I'm trying to parse a json string embedded in my html file.
Here is the reduced code.
<html>
<head>
<script src="./jquery-1.4.4.min.js" type="text/javascript"></script>
<script>
function parse_json(){
var jtext = $("#mtxt").text();
var jdata = jQuery.parseJSON(jtext);
JSON.parse(JSON.stringify(jdata), function (key, value){
alert("key=" + key + " value=" + value);
if(key== ""){
alert("value in string" + JSON.stringify(value));
}
});
}
$(document).ready(function() {
$("#run").click( function () {
parse_json();
});
});
</script>
</head>
<body>
<a id="run" href="#">run</a>
<div id="mtxt">
{"caller": "539293493"}
</div>
</body>
</html>
When I parse it, apart from the expected "caller" value, I get an extra empty "key" and "value".
The first alert gives me
key= value=[object Object]
The second alert gives me
value in string{}
What is happening? Why this extra entry?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,您将第二个参数传递给 JSON.parse(),这是
reviver
回调。根据 JSON 文档,此回调执行“...对于每个键和值 的结果替换。这可用于将通用对象重组为伪类实例,或将日期字符串转换为 Date 对象。”最终结果的级别。每个值都将被 reviver函数 >reviver 回调不会返回任何内容,您的对象被不当操作和扭曲。我不相信您在这里使用
reviver
有任何用处。我从未见过它在任何地方使用过,而且我经常使用JSON.parse
。您的代码应如下所示:
演示: http://jsfiddle.net/hjVqf/
Ok, you're passing the second param to JSON.parse() which is the
reviver
callback. Per the JSON docs, This callback is executed "...for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects."Since your
reviver
callback doesn't return anything, your object is getting improperly manipulated and distorted. I don't believe you have any use for thereviver
in your use here. I have never seen it in use anywhere, and I useJSON.parse
a LOT.Your code should look like this:
Demo: http://jsfiddle.net/hjVqf/
好吧,我一直在 jsfiddle 上玩 this 。我注意到您没有做的一件事是为
reviver
函数返回一个值。根据 Microsoft JSON.parse 文档< /a>,该函数的要点是返回value
属性的修改(如果需要)版本,该版本将更新 DOM 对象。现在,它还说:好的,所以我认为这里的关键是该函数运行两次的原因是因为它为第一个成员运行(简单地
"caller": "539293493"
),然后为对象本身运行({“来电者”:“539293493”}
)。您会注意到,在我的链接示例中,通过添加的
return value;
语句,带有空白键的对象是整个对象。Ok, I've been fooling around with this on jsfiddle. One of the things I noticed that you weren't doing, was returning a value for the
reviver
function. According to the Microsoft JSON.parse docs, the point of the function is to return a modified (if necessary) version of thevalue
property which will update the DOM object. Now, it also says that:Ok, so I think the key here is that the reason the function is run twice, is because it's running for the first member (simply
"caller": "539293493"
) and then for the object itself ({"caller": "539293493"}
).You'll notice that in my linked example, with the added
return value;
statement, the object with the blank key is the whole object.