json multidimensional array

C

Chris

I can't figure out how to handle multidimensional arrays with json
created with ajax/php e.g.

{"452":{"name":"chris","age":"21"},"798":{"name":"sam","qty":"22"}}

I use the following code for simple arrays but don't know how to
handle multidimensional arrays?

json_object = eval( "(" + xmlHttp.responseText + ")" );
document.getElementById('name').value= json_object["name"];

How do I get to json_object[key][name]?

Many thanks,

Chris
 
J

Jeremy J Starcher

I can't figure out how to handle multidimensional arrays with json
created with ajax/php e.g.

{"452":{"name":"chris","age":"21"},"798":{"name":"sam","qty":"22"}}

That is an OBJECT notation. Not an array. Although at times they seem
to work the same, they are -very- different things.

You would be best to read up on object notation and object properties and
how they differ from arrays.

try this:

<html>
<head>
<title>Object example</title>
</head>

<body>

<p>An example of how to look at an object properties. VERY basic, only
to be used as a starting point.<p>
<p>While may appear that properties will be listed in the same order they
are
defined, this is NOT assured.</p>

<script type="text/javascript">
// Create an object.
var o = {"First name" : "Jeremy",
"Last Name" : "Starcher",
"Age" : "XX"
};

// Create an array (1D of course)
var a = [1,2,3,"Hi"];

var key;

for (key in o) {
// Most modern browsers should have hasOwnProperty by now.
// This keeps us from getting farther up the chain.
if (o.hasOwnProperty(key)) {
document.write(key + "->" + o[key]);
document.write("<br>");
}
}
</script>
</body>
</html>
 
J

Joost Diepenmaat

Chris said:
I can't figure out how to handle multidimensional arrays with json
created with ajax/php e.g.

{"452":{"name":"chris","age":"21"},"798":{"name":"sam","qty":"22"}}

I use the following code for simple arrays but don't know how to
handle multidimensional arrays?

json_object = eval( "(" + xmlHttp.responseText + ")" );
document.getElementById('name').value= json_object["name"];

How do I get to json_object[key][name]?

key = "798"

var name = json_object[key]["name"]
 
P

purcaholic

I can't figure out how to handle multidimensional arrays with json
created with ajax/php e.g.

{"452":{"name":"chris","age":"21"},"798":{"name":"sam","qty":"22"}}

I use the following code for simple arrays but don't know how to
handle multidimensional arrays?

json_object = eval( "(" + xmlHttp.responseText + ")" );
document.getElementById('name').value= json_object["name"];

How do I get to json_object[key][name]?

Many thanks,

Chris

Try following way:
Code:
json_object['452'].name
json_object['452'].age
....

Regards,
purcaholic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,138
Messages
2,570,801
Members
47,348
Latest member
nethues

Latest Threads

Top