简体   繁体   中英

Convert JSON-like String to JSON

I have a String that describes an object and want to convert it into a JS Object.

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?

It does look more like actual real javascript than 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:

 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! In other words, don't do this unless you're 100% sure.

 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 .

I have a String that describes an object and want to convert it into a JS Object.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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