2013-06-20 2 views
0

У меня есть JSON в JavaScript, как это:Как добавить атрибут в JSON в Javascript?

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;