Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert json data to javascript array - in multi-dimensional sense

I have a json array say

{
   "People": {
       "Person": [
          {"FirstName": "John", "LastName": "Smith"}
          {"FirstName": "Joe", "LastName": "Bloggs"}
          {"FirstName": "Wendy", "LastName": "Deng"}
        ]
    }
}

And I want to convert this into a javascript array (something like this)

var persons = [ ["FirstName", "John", "LastName", "Smith"], ["FirstName", "Joe", "LastName", "Bloggs"], ["FirstName", "Wendy", "LastName": "Deng"] ];

How do I accomplish this? Hope my question makes sense and I realise the javascript array initialization may not be the correct way to put it.

Thanks.

like image 713
awongCM Avatar asked Dec 11 '25 01:12

awongCM


1 Answers

jsfiddle demo: here

var src={
   "People": {
       "Person": [
          {"FirstName": "John", "LastName": "Smith"},
          {"FirstName": "Joe", "LastName": "Bloggs"},
          {"FirstName": "Wendy", "LastName": "Deng"}
        ]
    }
};

var persons=[];
var obj=src["People"]["Person"];
for(i in obj){
  var temp=[];
  temp.push("FirstName");
  temp.push(obj[i].FirstName);
  temp.push("LastName");
  temp.push(obj[i].LastName);
  persons.push(temp);
  }

// persons contain your requried array
like image 158
Umesh Patil Avatar answered Dec 12 '25 16:12

Umesh Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!