create javascript array from complex json object
Possible Duplicate:
JavaScript “For …in” with Arrays
I am pretty new to JSON. I have searched for an answer on stackoverflow but am kind of confused so I'm not sure if I've stumbled upon a solution or not. So, I decided to post my code and needs in hopes for a solution that I can apply.
If there is already a solution that exists, I apologize in advance and can only imagine that I just didn't understand what I was looking at.
I have a json object which looks like this:
{
"0": [
{
"uguid": "11111-11111-11111",
"username": "username",
"password": "",
"first_name": "User1",
"last_name": "Name1",
"title": "title here",
"group_guid": "g0001",
"force_pw_reset": "f",
"permissions": [
[
"p1",
"p2",
"p3",
"p4",
"p5"
]
],
"active": "t",
"t_access": [
[]
],
"i_access": [
[]
],
"c_access": [
[]
],
"class_name": "class name",
"method": "method name"
}
],
"1": [
{
"uguid": "22222-22222-2222",
"username": "username2",
"password": "",
"first_name": "User2",
"last_name": "Name2",
"title": "",
"g_guid": "g0002",
"force_pw_reset": "f",
"permissions": [
[
"p1",
"p2",
"p3",
"p4",
"p5",
"p6"
]
],
"active": "t",
"t_access": [
[]
],
"i_access": [
[]
],
"c": [
[
"c0001"
]
],
"class_name": "class name",
"method": "method name"
}
],
"2": [
{
"u_guid": "33333-33333-33333",
"username": "username3",
"password": "",
"first_name": "User3",
"last_name": "Pass3",
"title": "",
"gguid": "g0003",
"force_pw_reset": "f",
"permissions": [
[
"p1",
"p2",
"p3"
]
],
"active": "t",
"t_access": [
[
"t0001"
]
],
"i_access": [
[]
],
"c_access": [
[]
],
"class_name": "class name",
"method": "method name"
}
]
}
What I want to be able to do is iterate over this (or any) json and create an array. I would need to be able to recursively iterate when/where needed. Also, hopefully whatever the solution is would work with whatever json object that gets passed to the function.
UPDATE: I guess I should have mentioned I want to create a javascript array if that makes any difference. Also, I am taking a json string and using json_parse() from douglas crockford to get an object. I want to take that object (shown) and create the javascript array.
If you want to convert an object
obj = {
"0": ...,
"1": ...,
...
}
to an array, I'd suggest this :
var a = [];
for (var i=0; typeof obj[i] != 'undefined'; i++) a[i] = obj[i];
链接地址: http://www.djcxy.com/p/70030.html