PDA

View Full Version : How to convert two dimensional array to JSON



livinglegends
5 Oct 2010, 12:15 AM
Hi,

I need to convert two dimensional array to JSON.

I can convert 1-d array to JSON but not 2-d array.

Below is the code I have written.


var foodArray = new Array();
var key = "custodian";
var obj = {};
foodArray[key] = ['Dal', 'abc'];
obj.val = foodArray;
key = "doc";
foodArray[key] = ['Dal1', 'abc1'];
obj.val = foodArray;
key = "lang";
foodArray[key] = ['Dal2', 'abc2'];

obj.val = foodArray;

var foodJson = Ext.encode(obj);
console.log(foodJson);Can anyone give idea?

Condor
5 Oct 2010, 12:26 AM
In javascript or in your server language?

Maybe an example of your data and a description of your desired output would help?

livinglegends
5 Oct 2010, 12:26 AM
In javascript.. I have updated my previous post with code which I am using.

Condor
5 Oct 2010, 12:29 AM
Arrays have Numbers as keys, Objects have Strings as keys.

So in your example, foodArray shouldn't be an Array at all, it should be an Object.

livinglegends
5 Oct 2010, 12:31 AM
it will be quite difficult now to convert it from Array to object as it is being used at so many places.

Is there any workaround to convert ?

Condor
5 Oct 2010, 12:39 AM
No, there isn't.

Also, you should not be using new Array or new Object.

You could use:

var foodArray = {};
foodArray["custodian"] = ['Dal', 'abc'];
foodArray["doc"] = ['Dal1', 'abc1'];
foodArray["lang"] = ['Dal2', 'abc2'];
obj = {};
obj.val = foodArray;
or even:

obj = {
val: {
custodian: ['Dal', 'abc'],
doc: ['Dal1', 'abc1'],
lang: ['Dal2', 'abc2']
}
};

livinglegends
5 Oct 2010, 1:29 AM
No, there isn't.

Also, you should not be using new Array or new Object.

You could use:

var foodArray = {};
foodArray["custodian"] = ['Dal', 'abc'];
foodArray["doc"] = ['Dal1', 'abc1'];
foodArray["lang"] = ['Dal2', 'abc2'];
obj = {val: foodArray};or even:

obj = {
val: {
custodian: ['Dal', 'abc'],
doc: ['Dal1', 'abc1'],
lang: ['Dal2', 'abc2']
}
};


Thanks a lot condor,

My problem get resolved...

really gr8 man you are...

kanmanithiru26
17 Sep 2012, 10:27 PM
Hi Mr.Condor...i ahve seen yr replies for the queries...so i think u r the apt person to clear my doubt.im new to android..so can u pls help me to help me how to separate the objects from the following which has an multidimentional array

String menu =
{"items":
{"item":
[
{"id": "0001","type": "donut","name": "Cake",

"topping":
[{ "id": "5001", "type": "cheese" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" }
]
},

{"id": "0002","type": "DairyMilk","name": "Choclate",

"topping":
[{ "id": "5008", "type": "butter" },
{ "id": "5009", "type": "cashew" },
{ "id": "5010", "type": "Sugar" }
]
}
]
}
}



I need the output to be something like this. i have to do this with Json in android eclipse..can u plz help me..thank u


Name : cake
id : 0001
type : donut

topping :
5001 - butter
5009 - cashew
5005 - sugar


Name : choclate
id:0002
type:dairymilk

topping:
5008 - butter
5009 - cashew
5010 - sugar