如何串的JavaScript字符串轉換爲 「{7:{類型: '行'}}」 爲目標
"{7: {type: 'line'},8: {type: 'line'}}"
轉換成使用JavasScript OBJECT
{7: {type: 'line'},8: {type: 'line'}}
如何串的JavaScript字符串轉換爲 「{7:{類型: '行'}}」 爲目標
"{7: {type: 'line'},8: {type: 'line'}}"
轉換成使用JavasScript OBJECT
{7: {type: 'line'},8: {type: 'line'}}
使用JSON.parse
JSON.parse(STRING) // convert to object
JSON.stringify(STRING) // convert object to string, that can be used in JSON.parse
不要使用數字作爲鍵,因爲它不好的做法,你不能訪問像這樣的對象:
var a = {"8": "value"};
// Invalid syntax
console.log(a.8)
// work
console.log(a["8"], a[8]);
var b = {"eight" : "value"};
// work
console.log(b.eight);
無法正常工作,原始字符串無效JSON。 – joscarsson
是的,因爲它無效json –
它的工作原理,你是對的字符串是無效的,我改變了它 'var lineasstr ='{「4」:{「type」:「line」},「5 「:{」type「:」line「}}'; JSON.parse(lineasstr);' 它與JSON.parse一起使用 – user3217054
是的,我看到這篇文章,但它並沒有爲我工作。我試圖使用谷歌ComboChart,他們使用這樣的選項: '系列:{4:{type:「line」},5:{type:「line」}}' 我建立字符串 '4:{type:「line」},5:{type:「line」}' from my program,but I can not convert into into a'option'value for googlecombochart: https:// google-developers。 appspot.com/chart/interactive/docs/gallery/combochart – user3217054