0

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
3
  • The content of the json array is string or from some response?. Please explain more Commented May 31, 2013 at 9:30
  • And do you mean to access that content in JavaScript? Just parse it and you can access object.Name or object.data[0].company[0].company_id etc... Commented May 31, 2013 at 9:32
  • You cant use the property Name twice, you have to use unique property names. Commented May 31, 2013 at 9:38

3 Answers 3

1

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

answered May 31, 2013 at 10:18
Sign up to request clarification or add additional context in comments.

Comments

0

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));
Flexo - Save the data dump
89.1k22 gold badges205 silver badges286 bronze badges
answered May 31, 2013 at 11:47

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.