1

imagine you have the following JSON:

{"zero":{"0":"foo","1":"foo2"},"one":"fooa","two":"foob"}

What is the most efficient way to convert it to :

 var zero = ['foo','foo2'];
 var one = 'fooa';
 var two = 'foob';
4
  • Probably start off by changing it to: var obj = {"zero":["foo","foo2"],"one":"fooa","two":"foob"} And then be all like obj.zero or obj.one or obj.two Commented Oct 19, 2012 at 22:29
  • 1
    Why do you want to use variables when you've got everything nicely organized in a data structure? Commented Oct 19, 2012 at 22:45
  • Because I want to convert this to HTML Commented Oct 19, 2012 at 23:07
  • @PedroEsperança: That doesn't really make sense. What does HTML have to do with your data references? Commented Oct 19, 2012 at 23:53

1 Answer 1

3

JSON.parse:

var json = '{"zero":{"0":"foo","1":"foo2"},"one":"fooa","two":"foob"}';
var pJson = JSON.parse(json);
var zero = [ pJson.zero[0], pJson.zero[1] ];
var one = pJson.one;
var two = pJson.two;
Sign up to request clarification or add additional context in comments.

1 Comment

could you help me visualize how to integrate this with a while? So that we can create an function that can do this to any kind of multidimensional array

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.