简体   繁体   English

将类似 JSON 的字符串转换为 JSON

[英]Convert JSON-like String to JSON

I have a String that describes an object and want to convert it into a JS Object.我有一个描述对象的字符串,并希望将其转换为 JS 对象。

String:细绳:

{
    "platform": "desktop",
    pageName: "JD Sports - Nike Air Force 1  07 LV8 Herren", //Page Title
    pageType: "product", //Page Type
    plu: "16085947_jdsportsde", //Product Code
    description: "Nike Air Force 1  07 LV8 Herren", //Product Name
    unitPrice: "110.00", //Product Price
                    category: "Herren \u003E Herrenschuhe \u003E Sneakers", //End level category
    categoryId: "jdsportsde_ct81275jdsportsde_ct81279jdsportsde_ct81356jdsportsde", //End level category Id
            sale: false, //is on sale? true/false
    brand: "Nike", //Product Brand
    ownbrand: false, //own brand product? true/false
    exclusive: false, //exclusive product? true/false
    onlineexlusive: false, //online exlcusive product? true/false
    currency:"EUR",
    variants: [
                                                            {
                            name:"41",
                            upc: "0194501189583",
                            page_id_variant: "16085947_jdsportsde.0194501189583"
                    }
                                    ,                       {
                            name:"42.5",
                            upc: "0194501189606",
                            page_id_variant: "16085947_jdsportsde.0194501189606"
                    }
                                    ,                       {
                            name:"45",
                            upc: "0194501189644",
                            page_id_variant: "16085947_jdsportsde.0194501189644"
                    }
                                    ,                       {
                            name:"47",
                            upc: "0194501189675",
                            page_id_variant: "16085947_jdsportsde.0194501189675"
                    }
                                    ,                       {
                            name:"47.5",
                            upc: "0194501189682",
                            page_id_variant: "16085947_jdsportsde.0194501189682"
                    }
                                    ,                       {
                            name:"48.5",
                            upc: "0194501189699",
                            page_id_variant: "16085947_jdsportsde.0194501189699"
                    }
                                    ,                       {
                            name:"L",
                            upc: "0194501189569",
                            page_id_variant: "16085947_jdsportsde.0194501189569"
                    }
    ]
};

Because the keys arent in quotation marks and because of the comments I can't use JSON.parse() How can i convert my String into an object?因为键不在引号中,并且由于注释我不能使用 JSON.parse() 如何将我的字符串转换为对象?

It does look more like actual real javascript than JSON.它看起来更像是真正的 JavaScript 而不是 JSON。

Although it is often considered evil , you could use eval to parse (and execute!) it, at the risk of running uncontrolled script, either in your user's browsers or in your backend:虽然它通常被认为是evil ,但您可以使用eval来解析(并执行!)它,冒着在用户浏览器或后端运行不受控制的脚本的风险:

 var str = `{ "platform": "desktop", pageName: "JD Sports - Nike Air Force 1 07 LV8 Herren", //Page Title pageType: "product", //Page Type plu: "16085947_jdsportsde", //Product Code description: "Nike Air Force 1 07 LV8 Herren", //Product Name unitPrice: "110.00", //Product Price category: "Herren \> Herrenschuhe \> Sneakers", //End level category categoryId: "jdsportsde_ct81275jdsportsde_ct81279jdsportsde_ct81356jdsportsde", //End level category Id sale: false, //is on sale? true/false brand: "Nike", //Product Brand ownbrand: false, //own brand product? true/false exclusive: false, //exclusive product? true/false onlineexlusive: false, //online exlcusive product? true/false currency:"EUR", variants: [ { name:"41", upc: "0194501189583", page_id_variant: "16085947_jdsportsde.0194501189583" } , { name:"42.5", upc: "0194501189606", page_id_variant: "16085947_jdsportsde.0194501189606" } , { name:"45", upc: "0194501189644", page_id_variant: "16085947_jdsportsde.0194501189644" } , { name:"47", upc: "0194501189675", page_id_variant: "16085947_jdsportsde.0194501189675" } , { name:"47.5", upc: "0194501189682", page_id_variant: "16085947_jdsportsde.0194501189682" } , { name:"48.5", upc: "0194501189699", page_id_variant: "16085947_jdsportsde.0194501189699" } , { name:"L", upc: "0194501189569", page_id_variant: "16085947_jdsportsde.0194501189569" } ] };`; eval("obj = " + str); console.log(obj.pageName);

First off you could use eval if you absolutely trust the source as it looks more like JS than JSON!首先,如果您绝对信任源代码,则可以使用 eval,因为它看起来更像 JS 而不是 JSON! In other words, don't do this unless you're 100% sure.换句话说,除非您 100% 确定,否则不要这样做。

 var s = `{ "platform": "desktop", pageName: "JD Sports - Nike Air Force 1 07 LV8 Herren", //Page Title pageType: "product", //Page Type plu: "16085947_jdsportsde", //Product Code description: "Nike Air Force 1 07 LV8 Herren" //Product Name }`; var obj; eval('obj = ' + s); console.log(obj.pageType); // and convert it to valid JSON var json = JSON.stringify(obj); console.log('JSON: ', json);

The best solution would be to fix the code that gives you this string, rather than try to handle an ill-formatted string after it was created.最好的解决方案是修复为您提供此字符串的代码,而不是在创建后尝试处理格式错误的字符串。 Prefer fixing the causes rather than the consequences.更喜欢解决原因而不是结果。

If that is not possible or if for any reason you don't want to do this, then the only solution is to use a little trick:如果这是不可能的,或者由于任何原因您不想这样做,那么唯一的解决方案是使用一个小技巧:

const string = `{
    "platform": "desktop",
    pageName: "JD Sports - Nike Air Force 1  07 LV8 Herren", //Page Title
    pageType: "product", //Page Type
    plu: "16085947_jdsportsde", //Product Code
    description: "Nike Air Force 1  07 LV8 Herren", //Product Name
    unitPrice: "110.00", //Product Price
                    category: "Herren > Herrenschuhe > Sneakers", //End level category
    categoryId: "jdsportsde_ct81275jdsportsde_ct81279jdsportsde_ct81356jdsportsde", //End level category Id
            sale: false, //is on sale? true/false
    brand: "Nike", //Product Brand
    ownbrand: false, //own brand product? true/false
    exclusive: false, //exclusive product? true/false
    onlineexlusive: false, //online exlcusive product? true/false
    currency:"EUR",
    variants: [
                                                            {
                            name:"41",
                            upc: "0194501189583",
                            page_id_variant: "16085947_jdsportsde.0194501189583"
                    }
                                    ,                       {
                            name:"42.5",
                            upc: "0194501189606",
                            page_id_variant: "16085947_jdsportsde.0194501189606"
                    }
                                    ,                       {
                            name:"45",
                            upc: "0194501189644",
                            page_id_variant: "16085947_jdsportsde.0194501189644"
                    }
                                    ,                       {
                            name:"47",
                            upc: "0194501189675",
                            page_id_variant: "16085947_jdsportsde.0194501189675"
                    }
                                    ,                       {
                            name:"47.5",
                            upc: "0194501189682",
                            page_id_variant: "16085947_jdsportsde.0194501189682"
                    }
                                    ,                       {
                            name:"48.5",
                            upc: "0194501189699",
                            page_id_variant: "16085947_jdsportsde.0194501189699"
                    }
                                    ,                       {
                            name:"L",
                            upc: "0194501189569",
                            page_id_variant: "16085947_jdsportsde.0194501189569"
                    }
                    ]};`
let object

eval("object = " + string)

This will create an object based on string .这将创建一个基于string的对象。

I have a String that describes an object and want to convert it into a JS Object.我有一个描述对象的字符串,并想将其转换为JS对象。

String:细绳:

{
    "platform": "desktop",
    pageName: "JD Sports - Nike Air Force 1  07 LV8 Herren", //Page Title
    pageType: "product", //Page Type
    plu: "16085947_jdsportsde", //Product Code
    description: "Nike Air Force 1  07 LV8 Herren", //Product Name
    unitPrice: "110.00", //Product Price
                    category: "Herren \u003E Herrenschuhe \u003E Sneakers", //End level category
    categoryId: "jdsportsde_ct81275jdsportsde_ct81279jdsportsde_ct81356jdsportsde", //End level category Id
            sale: false, //is on sale? true/false
    brand: "Nike", //Product Brand
    ownbrand: false, //own brand product? true/false
    exclusive: false, //exclusive product? true/false
    onlineexlusive: false, //online exlcusive product? true/false
    currency:"EUR",
    variants: [
                                                            {
                            name:"41",
                            upc: "0194501189583",
                            page_id_variant: "16085947_jdsportsde.0194501189583"
                    }
                                    ,                       {
                            name:"42.5",
                            upc: "0194501189606",
                            page_id_variant: "16085947_jdsportsde.0194501189606"
                    }
                                    ,                       {
                            name:"45",
                            upc: "0194501189644",
                            page_id_variant: "16085947_jdsportsde.0194501189644"
                    }
                                    ,                       {
                            name:"47",
                            upc: "0194501189675",
                            page_id_variant: "16085947_jdsportsde.0194501189675"
                    }
                                    ,                       {
                            name:"47.5",
                            upc: "0194501189682",
                            page_id_variant: "16085947_jdsportsde.0194501189682"
                    }
                                    ,                       {
                            name:"48.5",
                            upc: "0194501189699",
                            page_id_variant: "16085947_jdsportsde.0194501189699"
                    }
                                    ,                       {
                            name:"L",
                            upc: "0194501189569",
                            page_id_variant: "16085947_jdsportsde.0194501189569"
                    }
    ]
};

Because the keys arent in quotation marks and because of the comments I can't use JSON.parse() How can i convert my String into an object?由于键在引号中并且由于注释而不能使用JSON.parse(),如何将String转换为对象?

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

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