I have this as a string. I have get this using jquery and parsing it from a web page.
我把它作为一个字符串。我使用jquery并从网页解析它。
I tried using jQuery.parseJSON and got the error Uncaught SyntaxError: Unexpected token n
.
我尝试使用jQuery.parseJSON并得到错误Uncaught SyntaxError:Unexpected token n。
I need to get "surl" and "imgurl" how can I get this?
我需要得到“surl”和“imgurl”我怎么能得到这个?
{
ns:"images",
k:"5061",
mid:"172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F",
surl:"http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/",
imgurl:"http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif",
ow:"480",
docid:"608038082569896450",
oh:"301",
tft:"117",
dls:"images,5487"
}
Source of this string:
这个字符串的来源:
<a href="#" q="{ns:"images",k:"5061",mid:"172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F",surl:"http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/",imgurl:"http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif",ow:"480",docid:"608038082569896450",oh:"301",tft:"117",dls:"images,5487"}"></a>
4 个解决方案
#1
My example assumes your anchor has an id for convenience. If you use wrxsti's answer without all the unnecessary escaping, you get this:
我的例子假设你的锚有一个方便的id。如果你使用wrxsti的答案而没有所有不必要的转义,你得到这个:
var str = document.getElementById('a').getAttribute('q');
var json = JSON.stringify(eval("(" + str + ")"));
var obj = JSON.parse(json)
var surl = obj.srl;
But you really should get that attribute data changed to proper parsing JSON.
但是你真的应该将属性数据改为正确解析JSON。
#2
If you do not have the option of editing the JSON information you can do this to make it a valid json object. I will say that including eval can be bad due to security reasons, but this will make your JSON a valid object in the long run.
如果您没有编辑JSON信息的选项,则可以执行此操作以使其成为有效的json对象。我会说,由于安全原因,包括eval可能会很糟糕,但从长远来看,这将使你的JSON成为一个有效的对象。
var object = '{ \
ns: "images", \
k: "5061", \
mid: "172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F", \
surl: "http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/", \
imgurl: "http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif", \
ow: "480", \
docid: "60808082569896450", \
oh: "301", \
tft: "117", \
dls: "images5487" \
}';
var json = JSON.stringify(eval("(" + object + ")"));
var o = $.parseJSON(json);
Hope this helps. Let me know if you have any questions!
希望这可以帮助。如果您有任何疑问,请告诉我!
#3
this is a valid your JSON string :
这是一个有效的JSON字符串:
{
"ns": "images",
"k": "5061",
"mid": "172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F",
"surl": "http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/",
"imgurl": "http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif",
"ow": "480",
"docid": "608038082569896450",
"oh": "301",
"tft": "117",
"dls": "images,5487"
}
if you get some error in your JSON string then check its validation online through http://jsonlint.com/ i hope it will help you.
如果您的JSON字符串中出现错误,请通过http://jsonlint.com/在线查看其验证,我希望它能为您提供帮助。
#4
You use object
format not json
format, it is correct if you do like the following code:
您使用对象格式而不是json格式,如果您喜欢以下代码,则它是正确的:
var obj = {
ns:"images",
k:"5061",
mid:"172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F",
surl:"http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/",
imgurl:"http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif",
ow:"480",
docid:"608038082569896450",
oh:"301",
tft:"117",
dls:"images,5487"
}
Therefore, if you want to get surl
and imgurl
: just do: obj.surl;
and obj.imgurl
因此,如果你想获得surl和imgurl:只需:obj.surl;和obj.imgurl
#1
My example assumes your anchor has an id for convenience. If you use wrxsti's answer without all the unnecessary escaping, you get this:
我的例子假设你的锚有一个方便的id。如果你使用wrxsti的答案而没有所有不必要的转义,你得到这个:
var str = document.getElementById('a').getAttribute('q');
var json = JSON.stringify(eval("(" + str + ")"));
var obj = JSON.parse(json)
var surl = obj.srl;
But you really should get that attribute data changed to proper parsing JSON.
但是你真的应该将属性数据改为正确解析JSON。
#2
If you do not have the option of editing the JSON information you can do this to make it a valid json object. I will say that including eval can be bad due to security reasons, but this will make your JSON a valid object in the long run.
如果您没有编辑JSON信息的选项,则可以执行此操作以使其成为有效的json对象。我会说,由于安全原因,包括eval可能会很糟糕,但从长远来看,这将使你的JSON成为一个有效的对象。
var object = '{ \
ns: "images", \
k: "5061", \
mid: "172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F", \
surl: "http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/", \
imgurl: "http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif", \
ow: "480", \
docid: "60808082569896450", \
oh: "301", \
tft: "117", \
dls: "images5487" \
}';
var json = JSON.stringify(eval("(" + object + ")"));
var o = $.parseJSON(json);
Hope this helps. Let me know if you have any questions!
希望这可以帮助。如果您有任何疑问,请告诉我!
#3
this is a valid your JSON string :
这是一个有效的JSON字符串:
{
"ns": "images",
"k": "5061",
"mid": "172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F",
"surl": "http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/",
"imgurl": "http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif",
"ow": "480",
"docid": "608038082569896450",
"oh": "301",
"tft": "117",
"dls": "images,5487"
}
if you get some error in your JSON string then check its validation online through http://jsonlint.com/ i hope it will help you.
如果您的JSON字符串中出现错误,请通过http://jsonlint.com/在线查看其验证,我希望它能为您提供帮助。
#4
You use object
format not json
format, it is correct if you do like the following code:
您使用对象格式而不是json格式,如果您喜欢以下代码,则它是正确的:
var obj = {
ns:"images",
k:"5061",
mid:"172E23D582B8C2A90F3FE9DC2B04A3ECD1D4F70F",
surl:"http://polyhouse2011.wordpress.com/2011/05/16/safety-precautions-2/",
imgurl:"http://polyhouse2011.files.wordpress.com/2011/05/safety_gear1.gif",
ow:"480",
docid:"608038082569896450",
oh:"301",
tft:"117",
dls:"images,5487"
}
Therefore, if you want to get surl
and imgurl
: just do: obj.surl;
and obj.imgurl
因此,如果你想获得surl和imgurl:只需:obj.surl;和obj.imgurl