JSON对象:
{
"users": {
"1403365934870556481": {
"userId": "1403365934863466416",
"id": "1403365934870556481",
"relationType": "CHILD",
"nickname": "Nameson",
"active": "1",
"created": "2014-06-21 16:52:14",
"relationDetails": {
"userId": "1403365934863466416",
"title": "UNSELECTED",
},
"hasFinanceAccount": true
},
"1406296959993232380": {
"userId": "1406296959978437904",
"id": "1406296959993232380",
"relationType": "CHILD",
"nickname": "Izzy",
"active": "1",
"created": "2014-07-25 15:02:39",
"relationDetails": {
"userId": "1406296959978437904",
"title": "UNSELECTED",
},
"hasFinanceAccount": true
}
}
}
对象的方案:
--> somenumber -> obj
obj->users
--> somenumber -> obj
我唯一能想到的就是将该对象解析为字符串并用其他东西替换该数字
任何建议?
我的解决方案是:
var tmp = JSON.stringify( currentPageData.getChildren );
tmp = t.replace(/[\d]+\"\:\{\"userId/g, 'child":{"userId');
myobj = JSON.parse(tmp);
答案 0 :(得分:3)
var myobj = {
3: "three"
}
console.log(myobj[3]) // prints "three"
答案 1 :(得分:1)
var obj = { 1234567890: 'test', asdf: 'okay' };
var keys = Object.keys(obj);
console.log(obj[keys[0]]); // 'test'
console.log(obj[keys[1]]); // 'okay'
var obj = JSON.parse(json);
var values = Object.keys(obj.users).map(function(key){ return obj.users[key]});
values.forEach(function(value){
if (value["userId"]){
console.log(value["nickname"]);
}
});