1

I have a JSON object which looks like this:

[{"tabname":"orders","datagroups":[{"dataname":"ordersToday","datavalue":9},{"dataname":"orders30Days","datavalue":126}]}] 

When I use console.log($.parseJSON(thedata)) I just get the word Object and no actual data.

How do I organise this data into a multidimensional javascript array? so that it looks something like this:

array("tabname"=>"orders", "datagroup"=>array(array("dataname"=>"ordersToday", "datavalue"=>9),array("dataname"=>"orders30Days","datavalue"=>126)))  
7
  • 1
    Click on the word Object in the console to see what it contains. Or do a console.log(JSON.stringify(parsedData)) Commented Jul 5, 2013 at 13:41
  • Try console.dir() instead. Commented Jul 5, 2013 at 13:46
  • Clicking on Object gives this: [Object] 0: Object length: 1 proto: Array[0] concat: function concat() { [native code] } constructor: function Array() { [native code] } every: function every() { [native code] } filter: function filter() { [native code] } forEach: function forEach() { [native code] } indexOf: function indexOf() { [native code] } join: function join() { [native code] } lastIndexOf: function lastIndexOf() { [native code] } length: 0 map: function map() { [native code] } pop: function pop() { [native code] } push: function push() { [native code] } ..plus plenty more of the same Commented Jul 5, 2013 at 13:56
  • console.dir() give me the original json data. I'm trying to have it converted to a javascript array Commented Jul 5, 2013 at 13:58
  • It is an array already. You only don't get the console printout. If you expand the 0 property you will see an object, if you expand that one's datagroups property you will get the inner arrayà Commented Jul 5, 2013 at 14:03

3 Answers 3

1

It is an array:

var json = '[{"tabname":"orders","datagroups":[{"dataname":"ordersToday","datavalue":9},{"dataname":"orders30Days","datavalue":126}]}]';

var obj = $.parseJSON(json);

Array.isArray(obj) // => true
0

It's quite simple, really.
You can simply use jQuery's $.parseJSON (jsonString).

0

Thanks to everyone for contributing. I took a break then came back and figured it out. The way my brain works is all wrong.

To access the individual values, I needed to do something like this:

var orderStats = $.parseJSON(data);
console.log(orderStats[0].datagroups[0].dataname);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.