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 acplish this? Hope my question makes sense and I realise the javascript array initialization may not be the correct way to put it.
Thanks.
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 acplish this? Hope my question makes sense and I realise the javascript array initialization may not be the correct way to put it.
Thanks.
Share Improve this question edited Nov 28, 2012 at 10:56 Usman 3,2783 gold badges30 silver badges49 bronze badges asked Nov 28, 2012 at 10:49 awongCMawongCM 9275 gold badges22 silver badges47 bronze badges 3- 1 Why would you want to do this? Why not just use the JSON as a JavaScript object? – Aesthete Commented Nov 28, 2012 at 11:05
- Two reasons: 1. It's out of my curiosity if there's any good array-like data structures that I can use to perform data manipulation inside the json such as binary search, binary sort etc versus javascript object. 2. I'm still a JSON newbie. – awongCM Commented Nov 28, 2012 at 11:18
-
First of all,
JSON
is a data interchange format, represented as a string.JSON
format can be mapped directly to JavaScript types, includingObject
andArray
. You question doesn't go into why you want to do this, and maybe that would help you to understand the uses of JSON and JavaScript objects. – Aesthete Commented Nov 28, 2012 at 11:24
2 Answers
Reset to default 1jsfiddle 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
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.
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?
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.
console.log(innerArray[0]); // Log the first element.
> 1 // First element is 1
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745271857a4619796.html
评论列表(0条)