How can i parse the fol开发者_开发知识库lowing json string without the opening and closing quotes? Its seems like javascript JSON.parse function is parsing the string with the quotes! hence throws a syntax error.
This is my string;
"[
{
"pk": 1,
"model": "pms.category",
"fields": {
"name": "Rent",
"add_date": "2011-07-28 01:33:21",
"agent": 3,
"category_type": "I",
"add_user": 3,
"desc": "Rent"
}
},
{
"pk": 2,
"model": "pms.category",
"fields": {
"name": "Deposit Rent",
"add_date": "2011-07-28 01:33:21",
"agent": 3,
"category_type": "I",
"add_user": 3,
"desc": "Rent Deposit"
}
}
]"
Edit: Something interesting is that when i run this string of my development machine, it is parsed correctly, but on the production server it fails.
Replace the opening and closing quotes, and then parse the string:
s = s.replace(/^"|"$/g, '');
var jsonDoc = JSON.parse(s);
Additionally, file a bug report with the author of the program or library that emits malformed JSON.
you should probably remove the outer quotes from your string...
mystring = mystring.replace(/^"|"$/g,'')
1.This should be your JSON format otherwise, it WILL NOT be parsed properly by JSON.parse
[
{
"pk": 1,
"model": "pms.category",
"fields": {
"name": "Rent",
"add_date": "2011-07-28 01:33:21",
"agent": 3,
"category_type": "I",
"add_user": 3,
"desc": "Rent"
}
},
{
"pk": 2,
"model": "pms.category",
"fields": {
"name": "Deposit Rent",
"add_date": "2011-07-28 01:33:21",
"agent": 3,
"category_type": "I",
"add_user": 3,
"desc": "Rent Deposit"
}
}
]
2.Include json2.js from the github repository inbetween your head tags.
精彩评论