将json数据转换为javascript数组——在多维意义上

时间:2021-03-28 21:28:10

I have a json array say

我有一个json数组。

{
   "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)

我想把它转换成javascript数组(类似这样)

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.

我如何做到这一点?希望我的问题有意义,我意识到javascript数组初始化可能不是正确的方式。

Thanks.

谢谢。

2 个解决方案

#1


1  

jsfiddle demo: here

jsfiddle演示:

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

#2


0  

What you're trying to do doesn't make alot of sense. Converting an existing valid JSON Object into the array format you specified is rather pointless, and will make accessing elements difficult.

你想做的事情没有什么意义。将现有的有效JSON对象转换为指定的数组格式是相当没有意义的,并且会使访问元素变得困难。

Sounds like what you really want to do is this:

听起来你真正想做的是:

var data = JSON.parse(jsonString); // Your JSON, parsed into a JS object.
var persons = []
data.people.person.foEach( function(el) {
  person.push(el.FirstName + " " + el.LastName);
}

Now persons look like this:

现在的人是这样的:

["John Smith", "Joe Bloggs", "Wendy Deng"]

Are you sure you know how JSON and JavaScript objects work?

您确定知道JSON和JavaScript对象是如何工作的吗?

var myJSON = '{ "foo": { "bar": { "baz" : [1,2,3] } } }' // JSON, String type.
var myObj = JSON.parse(myJSON); // Now a JS Object type.
var innerArray = myObj.foo.bar.baz;

Now the value of variable innerArray is [1,2,3], an Array type.

现在变量innerArray的值是[1,2,3],一个数组类型。

console.log(innerArray[0]); // Log the first element.

> 1 // First element is 1

#1


1  

jsfiddle demo: here

jsfiddle演示:

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

#2


0  

What you're trying to do doesn't make alot of sense. Converting an existing valid JSON Object into the array format you specified is rather pointless, and will make accessing elements difficult.

你想做的事情没有什么意义。将现有的有效JSON对象转换为指定的数组格式是相当没有意义的,并且会使访问元素变得困难。

Sounds like what you really want to do is this:

听起来你真正想做的是:

var data = JSON.parse(jsonString); // Your JSON, parsed into a JS object.
var persons = []
data.people.person.foEach( function(el) {
  person.push(el.FirstName + " " + el.LastName);
}

Now persons look like this:

现在的人是这样的:

["John Smith", "Joe Bloggs", "Wendy Deng"]

Are you sure you know how JSON and JavaScript objects work?

您确定知道JSON和JavaScript对象是如何工作的吗?

var myJSON = '{ "foo": { "bar": { "baz" : [1,2,3] } } }' // JSON, String type.
var myObj = JSON.parse(myJSON); // Now a JS Object type.
var innerArray = myObj.foo.bar.baz;

Now the value of variable innerArray is [1,2,3], an Array type.

现在变量innerArray的值是[1,2,3],一个数组类型。

console.log(innerArray[0]); // Log the first element.

> 1 // First element is 1