0

I need to parse a table to JSON, I found this solution and it works:

    var tab=[{"value":"1.0","label":"Alabama"},{"value":"2.0","label":"Alaska"},        {"value":"3.0","label":"American Samoa"}];
    var myJsonString = JSON.stringify(tab);  
    var jsonData = $.parseJSON(myJsonString);

The problem is when I declare dynamically the two dimensional table 'tab' it doesn't work:

    var tab_desc1= new Array(3);
    tab_desc1[0]=new Array(2);
    tab_desc1[0]["value"]="1.0";
    tab_desc1[0]["label"]="Alabama";
    tab_desc1[1]=new Array(2);
    tab_desc1[1]["value"]="2.0";
    tab_desc1[1]["label"]="Alaska";
    tab_desc1[2]=new Array(2);
    tab_desc1[2]["value"]="3.0";
    tab_desc1[2]["label"]="American Samoa";
    var myJsonString = JSON.stringify(tab_desc1);    
    var jsonData = $.parseJSON(myJsonString);

Logically my declaration contains error, bur I can't see it. Any help! Thanks.

Kimo_do
  • 568
  • 5
  • 12
  • 1
    Your inner dimension is _not_ an array, so don't do `tab_desc1[0] = new Array()`. It is an _object_ (JS arrays do not have string keys) so create it as one `tab_desc1[0] = {}` – Michael Berkowski Aug 17 '12 at 02:20
  • 1
    You are using arrays with strings. If you want to use strings use object. In your case you should use array and objects together. – Petar Sabev Aug 17 '12 at 02:21
  • i'm working a mobile application (jquery mobile and phoneGap), I need it for an auto complete search with complex object: http://www.andymatthews.net/code/autocomplete/local_complex.html – Kimo_do Aug 17 '12 at 02:22
  • possible duplicate of [JavaScript associative array to JSON](http://stackoverflow.com/questions/4425289/javascript-associative-array-to-json) – Felix Kling Aug 17 '12 at 02:24
  • "stringify()" to convert the array to json string ( I included a library script to use this method) and it works well in the case of the first delcatation – Kimo_do Aug 17 '12 at 02:27

2 Answers2

2
tab_desc1[0] = new Array(2);

Should be

tab_desc1[0] = {};

And same with others.

But I don't know the purpose of stringify a variable to string and then parse it back.

xdazz
  • 158,678
  • 38
  • 247
  • 274
1

The problem is that arrays and objects are not the same thing.

Your first code creates an array of objects.

Your second code creates an array of arrays, but then sets non-numeric properties on those arrays. JS arrays are a type of object so it is not an error to set non-numeric properties, but stringify() will only include the numeric properties. You need to do this:

var tab_desc1 = []; // Note that [] is "nicer" than new Array(3)
tab_desc1[0] = {};  // NOTE the curly brackets to create an object not an array
tab_desc1[0]["value"] = "1.0";
tab_desc1[0]["label"] = "Alabama";
tab_desc1[1] = {};  // curly brackets
tab_desc1[1]["value"] = "2.0";
tab_desc1[1]["label"] = "Alaska";
tab_desc1[2] = {};  // curly brackets
tab_desc1[2]["value"] = "3.0";
tab_desc1[2]["label"] = "American Samoa";

var myJsonString = JSON.stringify(tab_desc1);    
var jsonData = $.parseJSON(myJsonString);

You could also do this:

var tab_desc1 = [];
tab_desc1[0] = {"value":"1.0","label":"Alabama"};
tab_desc1[1] = {"value":"2.0","label":"Alaska"};
tab_desc1[2] = {"value":"3.0","label":"American Samoa"};

(Also, why stringify and then immediately parse to get back the same object?)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • because I read data from a file , I should browse it to insert data in the array and then parse it to JSON ( cause the script requires json format ) – Kimo_do Aug 17 '12 at 02:44