how to create this type of array in javascript?? my array structure as below
{
"Name":"Mr.X",
"Name":"Main Outlet",
"data":{
"company":{
"company_id":"5",
"company name":"texas LTD.",
"owner_name":"MR jack",
"owner_email":"[email protected]",
"owner_mobile":"999999",
"comp_product":[
{
"Productid" : "1",
"Productname" : "samsung"
},
{
"Productid" : "2",
"Productname" : "nokia"
}
]
}
}
}
asked May 31, 2013 at 9:23
Piyush
3,0919 gold badges36 silver badges52 bronze badges
3 Answers 3
use this site to check the validity of your json http://jsonformatter.curiousconcept.com/ and then you can use
JSON.parse('{"Name":"MrX"}') to give you a json object array
Sign up to request clarification or add additional context in comments.
Comments
first you cant keep sample key name in object like "Name"
var details = {};
details["Name"] = "Mr. X";
var company = {};
company["company_id"] = "5";
company["owner_name"] = "MR jack";
//company[...] = ...;
var company_product = [];
{
var comp_product = {};
comp_product["productid"] = 1;
comp_product["productname"] = "samsung";
company_product.push(comp_product);
}
{
var comp_product = {};
comp_product["productid"] = 2;
comp_product["productname"] = "nokia";
company_product.push(comp_product);
}
company["comp_product"] = comp_product;
details["data"] = company;
alert(JSON.stringify(details));
Comments
I'm not sure what are you trying to do there (you have only 1 array), but this might be a great help: http://www.jsonschema.net/
answered May 31, 2013 at 9:56
Balint Bako
2,5701 gold badge16 silver badges14 bronze badges
Comments
lang-js
object.Nameorobject.data[0].company[0].company_idetc...Nametwice, you have to use unique property names.