简体   繁体   English

需要使用JavaScript解析Json字符串

[英]need to parse Json string using javascript

I have my json value coming from java, as 我有来自Java的json值,

var obj = "[{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"},{"id":"58","str":"sd <p/> Comment By Smith John on 2012-10-30"}] ".

I want to parse using jquery as $.parseJSON(obj); 我想使用jquery解析为$ .parseJSON(obj);

I am getting error as undefined. 我收到未定义的错误。 I want to get the id value. 我想获取id值。

I included the jquery-ui.js and jquery.js. 我包括了jquery-ui.js和jquery.js。

I checked in the jsonlint and get it as valid json. 我检查了jsonlint并将其作为有效的json。

JavaScript strings containing " characters must be either delimited by ' character or have the " escaped as \\" . 包含"字符" JavaScript字符串必须以'字符分隔或以"转义为\\"

With your current code you should have errors trying to parse the JS that attempts to instantiate obj before it complains that obj is undefined . 使用当前代码,尝试解析试图实例化obj的JS之前,它会抱怨错误,即抱怨obj undefined

please add the escape params for the string first like the below. 请先为字符串添加转义参数,如下所示。

var obj = "[{\"id\":\"58\",\"str\":\"sd <p/> Comment By Smith John on 2012-10-30\"},{\"id\":\"58\",\"str\":\"sd <p/> Comment By Smith John on 2012-10-30\"}]"

and then take the parsed object to a variable and access the id in the below manner.. 然后将解析的对象带到一个变量,并以下面的方式访问id。

var tmp = $.parseJSON(obj); var tmp = $ .parseJSON(obj); tmp[1].id; tmp [1] .id;

hope you got the answer. 希望你得到答案。

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

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