2013-06-20 108 views
0

我JSON在Javascript中是這樣的:如何在Javascript中添加JSON屬性?

var json = { 
    "total": "100", 
    "page":"1", 
    "records":"100", 
    "rows": [ 
     { 
      "no": "1", 
      "part_number": "2", 
      "part_name": "3", 
      "price": "4", 
      "note": "8" 
     } 
    ] 
}; 

,我想補充

"test1":"5", 
"test2":"7" 

成JSON以上。

所以這將是這樣的:

var json = { 
    "total":"100", 
    "page":"1", 
    "records":"100", 
    "rows": [ 
     { 
      "no": "1", 
      "part_number": "2", 
      "part_name": "3", 
      "price": "4", 
      "test1": "5", 
      "test2": "7", 
      "note":"8" 
     } 
    ] 
}; 
+3

你有一個對象的文字,而不是JSON。 [瞭解有關對象的更多信息](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects)。也看看這個問題:[訪問/進程(嵌套)對象,數組或JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json)。 –

回答

2
json["rows"][0]["test1"] = "5"; 
json["rows"][0]["test2"] = "7"; 
2

剛:

json.rows[0].test1=5; // or if you want "5" 
json.rows[0].test2=7; 
相關問題