Amir Mursal
Amir Mursal

Reputation: 9

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; 

    }

Upvotes: 0

Views: 55

Answers (3)

kisp
kisp

Reputation: 6562

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 });

Upvotes: 0

Sridhar DD
Sridhar DD

Reputation: 1980

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]);
      }
    }
  }
}

Upvotes: 0

Jonas
Jonas

Reputation: 1345

Your JSON seems to be improperly formatted. If you do want to access the sub-elements of email as objects, your JSON should look like this:

 { "email" : { "FamilyMembers": 54,
            "Children": 4, 
            "Income": 8484, 
            "Literacy": "UG",
            "MaritalStatus": "Married"
          }
  }

Upvotes: 1

Related Questions