Click here to Skip to main content
14,921,476 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a JS array like this:
var
C#
Vehicles =  [[,],[,]];

Vehicles [0][0] = "SUV"
Vehicles [0][1] = "Chevy";
Vehicles [1][0] = "SEDAN";
Vehicles [1][1] = "Nissan";

I looked into how to convert a multidimensional array into json but I was wondering if it could be done in a more simple way. How do I convert above array into a json string like such:
CSS
{"Vehicles" : 
     [{"typeClass":"SUV" , "typeVehicle":"Chevy"},
      {"typeClass":"SEDAN" , "typeVehicle":"Nissan"}
     ]
}

I read I can use JSON.stringify(Vehicles). But the array is a cyclic datasctructure so that will not work. Should I just built the string and then parse it into a JSON object?
Such as
var Vehicles = "SUV,Chevy;SEDAN,Nissan";
var MyJSONObject = JSON.parse(Vehicles);


Thanks,
Robert
Posted
Updated 12-Dec-13 10:50am
v2
Comments
Sergey Alexandrovich Kryukov 12-Dec-13 14:53pm
   
Why? JSON is the adequate Javascript structure which should be used instead of string, not populated from a string. But you can do it, what's the problem?
—SA
Member 3493606 12-Dec-13 15:54pm
   
I am doing an AJAX request, the data would be my array (Vehicles), which needs to be in JSON format. Maybe I phrased it incorrectly. How do I create the JSON structure from that JS array?
Sergey Alexandrovich Kryukov 12-Dec-13 17:56pm
   
JSON IS a JS array... :-)
—SA
Member 3493606 16-Dec-13 10:54am
   
You are right, I figured it out.
Sergey Alexandrovich Kryukov 16-Dec-13 10:59am
   
Pleasure to deal with an inquirer who can actually use the advice and make things happen.
—SA
enhzflep 16-Dec-13 11:09am
   
Indeed. Unfortunately, something of a rarity.

1 solution

This will produce the output you've specified.

JavaScript
function Vehicle(class_t, type_t)
{
    this.typeClass = class_t;
    this.typeVehicle = type_t;
}

function test1()
{
	var tmp=new Array();
	tmp[0] = new Vehicle("SUV", "Chevy");
	tmp[1] = new Vehicle("SEDAN", "Nissan");
	
	myDataObject = new Object;
	myDataObject.Vehicles = tmp;
	
	objectJSON = JSON.stringify(myDataObject);
	console.log(objectJSON);
}


If you wished to convert your existing array, you could do so with a small function.

JavaScript
function test2()
{
    var Vehicles = new Array();
    Vehicles[0] = new Array("SUV", "Chevy");
    Vehicles[1] = new Array("SEDAN", "Nissan");
    Vehicles[2] = new Array("COUPE", "Mazda");
    Vehicles[3] = new Array("ESTATE", "Volvo");

    var i, n = Vehicles.length;
    var tmp = new Array();
    for (i=0; i<n; i++)
    {
        tmp[i] = new Vehicle( Vehicles[i][0], Vehicles[i][1] );
    }
    var mObj = new Object;
    mObj.Vehicles = tmp;
    var objJSON = JSON.stringify(mObj);
    console.log(objJSON);
}
   
Comments
Member 3493606 16-Dec-13 10:53am
   
I figured it out but thanks for the example.
Sergey Alexandrovich Kryukov 16-Dec-13 10:59am
   
5ed.
—SA
enhzflep 16-Dec-13 11:08am
   
Thank-you. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900