我该如何解析json字符串而无需使用开头和结尾的字符串引号? [英] How can i parse json string without the opening and closing string quotes?

查看:161
本文介绍了我该如何解析json字符串而无需使用开头和结尾的字符串引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何解析以下json字符串而无需使用引号和结尾引号?似乎javascript JSON.parse函数正在使用引号解析字符串!因此会引发语法错误.

How can i parse the following 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.

这是我的字符串;

"[
    {
        "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"
        }
    }
]"

有趣的是,当我运行开发计算机的此字符串时,它可以正确解析,但是在生产服务器上它将失败.

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);

此外,向发出格式错误的JSON的程序或库的作者提交错误报告.

Additionally, file a bug report with the author of the program or library that emits malformed JSON.

这篇关于我该如何解析json字符串而无需使用开头和结尾的字符串引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆