我剛剛開始使用這個,所以我對如何處理以下內容感到困惑。操縱JSON對象:如何引用和更新嵌套值
// so I believe this really isn't an object just a string representation, but for example please excuse the name
var dataObj = "{'id': 1,
'data': { 'color': 'red',
'shape' : 'triangle',
'height' : 100,
'width' : 45,
},
'id': 2,
'data': { 'color': 'blue',
'shape' : 'square',
'height' : 75,
'width' : 67,
},
'id': 3,
'data': { 'color': 'orange',
'shape' : 'circle',
'height' : 89,
'width' :24,
}
}";
等什麼我有一個問題是我怎麼會由ID(如SQL UPDATE WHERE東西的那種)更新的數據值的特定子集? javascript或jquery對我來說真的不重要,只是不知道任何一種方法。
dataObjUpdate(2);
function dataObjUpdate (passedID) {
//access the data by the passedID match and update the color to black
}
感激幫助傢伙....
JSON是一種數據序列化格式(在你的問題的字符串你的對象不是的方式有效的JSON,JSON使用雙引號)。您可以刪除引號,並且您將得到一個有效的JavaScript對象 –
數據格式更重要的問題是''id'屬性在序列化對象中多次出現。我想你可能想改變成一個對象數組,而不是這個對象。 – Sirko
是的,這是有道理的....不好的例子失敗:)。謝謝 – Justin