[英]How to access each element in the following JSON data?
"email": "FamilyMembers: 54; Children: 4; Income: 8484; Literacy: UG; MaritalStatus: Married;
I am Trying this code 我正在尝试此代码
if (arr.email != undefined) {
out += "Family Members:" + arr.email.FamilyMembers + "<br/>Children:" + arr.email.Children + "<br/>Income:" + arr.email.Income + "<br/>Literacy:" + arr.email.Literacy + "<br/>Marital Status:" + arr.email.MaritalStatus;
}
Your JSON seems to be improperly formatted. 您的JSON格式似乎不正确。 If you do want to access the sub-elements of email as objects, your JSON should look like this: 如果您确实希望将电子邮件的子元素作为对象访问,则JSON应该如下所示:
{ "email" : { "FamilyMembers": 54,
"Children": 4,
"Income": 8484,
"Literacy": "UG",
"MaritalStatus": "Married"
}
}
Following code will be enough. 以下代码就足够了。
var p = {"email": "FamilyMembers: 54; Children: 4; Income: 8484; Literacy: UG; MaritalStatus: Married;"}
for (var key in p) {
if (key == email) {
var emailElt = p[key]
for (var subKey in emailElt) {
if (p.hasOwnProperty(subKey)) {
alert(subKey + " -> " + emailElt[subKey]);
}
}
}
}
parse it, for example : 解析它,例如:
var myString = arr.email;
var arrayOfKV = myString.split(";");
var obj = {};
while( elem = arrayOfKV.shift() ) {
var kV = elem.split(":");
obj[k] = V;
}
var properJSON = JSON.stringify({ email: obj });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.