admin管理员组

文章数量:1355581

i have a plex object which is stored as a string in a text file

This is my data which i am reading from a text file

 [
 {
    name: "V",
    number: 20,
    coords: {
        "cn": { 
            points: [
                [23,32,32],
                [32,32,32]
            ], 
            B: "VC", 
            label: "ds"
        }
    }
}]

I want to convert this to a JSON string

Note:- I cant use eval function I have tried this JSON.stringify but i am getting this output:-

"   [\r\n     {\r\n        name: \"V\",\r\n        number: 20,\r\n        coords: {\r\n            \"cn\": { \r\n                points: [\r\n                    [23,32,32],\r\n                    [32,32,32]\r\n                ], \r\n                B: \"VC\", \r\n                label: \"ds\"\r\n            }\r\n        }\r\n    }]"

i have a plex object which is stored as a string in a text file

This is my data which i am reading from a text file

 [
 {
    name: "V",
    number: 20,
    coords: {
        "cn": { 
            points: [
                [23,32,32],
                [32,32,32]
            ], 
            B: "VC", 
            label: "ds"
        }
    }
}]

I want to convert this to a JSON string

Note:- I cant use eval function I have tried this JSON.stringify but i am getting this output:-

"   [\r\n     {\r\n        name: \"V\",\r\n        number: 20,\r\n        coords: {\r\n            \"cn\": { \r\n                points: [\r\n                    [23,32,32],\r\n                    [32,32,32]\r\n                ], \r\n                B: \"VC\", \r\n                label: \"ds\"\r\n            }\r\n        }\r\n    }]"
Share Improve this question edited Nov 19, 2020 at 20:36 Dmitry Reutov 3,0321 gold badge8 silver badges22 bronze badges asked Sep 30, 2015 at 18:36 bangali babubangali babu 1271 gold badge2 silver badges9 bronze badges 3
  • 2 JSON.stringify(eval(string)) – Pranav C Balan Commented Sep 30, 2015 at 18:39
  • 2 I suggest to store the data as JSON in the first place. – Felix Kling Commented Sep 30, 2015 at 18:45
  • JSON.parse could be an option: JSON.parse vs. eval – WhiteHat Commented Sep 30, 2015 at 19:00
Add a ment  | 

2 Answers 2

Reset to default 2

You can use bination of eval() and JSON.stringify(). eval() will convert it into valid JavaScript object and now you can use JSON.stringify() to convert it into JSON string.

var str='[\
 {\
    name: "V",\
    number: 20,\
    coords: {\
        "cn": { \
            points: [\
                [23,32,32],\
                [32,32,32]\
            ], \
            B: "VC", \
            label: "ds"\
        }\
    }\
}]';

document.write(JSON.stringify(eval(str)));

i don't know the reason for this! converting object-form-string to JSON-string but it's easy in your case:

let object_form_string = `[
 {
    name: "V",
    number: 20,
    coords: {
        "cn": { 
            points: [
                [23,32,32],
                [32,32,32]
            ], 
            B: "VC", 
            label: "ds"
        }
    }
}]`

let json_form_string = object_form_string.replace(/(name|number|coords|points|B|label)(?=:)/g, "\"$&\"")

let obj_json = JSON.parse(`{"coordination": ${json_form_string}}`)
// add {} to change array to object
// add "coordination" (just an example) to access to data
// read/write data (optional of course)
let json = JSON.stringify(obj_json)
console.log(json)
console.log(`type of obj_json is: ${typeof obj_json}`)
console.log(json_form_string)
console.log(`name is: ${obj_json.coordination[0].name}`)
console.log(`number is: ${obj_json.coordination[0].number}`)

enjoy and don't use eval()

本文标签: javascripthow to jsonstringify() a stringStack Overflow