我已被解析成稱爲JSONObj
一個對象,其被構造這樣的JSON文件contact.txt
:編輯解析JSON
[
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumbers": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
},
{
"firstName": "Mike",
"lastName": "Jackson",
"address": {
"streetAddress": "21 Barnes Street",
"city": "Abeokuta",
"state": "Ogun",
"postalCode": "10122"
},
"phoneNumbers": [
{ "type": "home", "number": "101 444-0123" },
{ "type": "fax", "number": "757 666-5678" }
]
}
]
我設想通過從一種形式,從而取入數據編輯文件/對象添加更多聯繫人。我怎樣才能做到這一點?
添加新的聯繫人到JSONObj
的陣列下面的方法似乎並不奏效,什麼問題?:
var newContact = {
"firstName": "Jaseph",
"lastName": "Lamb",
"address": {
"streetAddress": "25 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "13021"
},
"phoneNumbers": [
{ "type": "home", "number": "312 545-1234" },
{ "type": "fax", "number": "626 554-4567" }
]
}
var z = contact.JSONObj.length;
contact.JSONObj.push(newContact);
我根據您的回覆更新了我的答案。 – 2009-10-11 16:39:36
我已更新我的原始帖子以顯示我如何試圖操縱JSON對象。 – Fortisimo 2009-10-11 17:02:04