如何使用JS从JSON字符串解析参数? [英] How to parse parameters from JSON String using JS?
本文介绍了如何使用JS从JSON字符串解析参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析JSON字符串,但解析后的值是undefined.这是 jsfiddle
I'm trying to parse a JSON String , but the parsed value is undefined . Here is jsfiddle
<input type="submit" onclick=testJSON() value="Test"/>
<div id = "result">Test </div>
<script>
function testJSON() {
var jsonString = '{"param1":"123","param2":"XXX78","param3":"11378"}';
document.getElementById("result").innerHTML = (JSON.parse(jsonString)).param2;
}
</script>
推荐答案
代码运行正常-这里的问题在JSFiddle中,因为它被设置为仅在加载文档时运行JS代码,因此该功能无法正常运行.不注册-这是相关选项:
The code is working fine - the problem here is in JSFiddle, as it's set to just run the JS code when the document is loaded, so the function doesn't get registered - here's the relevant option:
这篇关于如何使用JS从JSON字符串解析参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文