Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"

           }
         ]
      }
   }
}
share|improve this question
 
The content of the json array is string or from some response?. Please explain more –  Dineshkani May 31 '13 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... –  RemarkLima May 31 '13 at 9:32
 
You cant use the property Name twice, you have to use unique property names. –  user1983983 May 31 '13 at 9:38
add comment

3 Answers

up vote 0 down vote accepted

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));
share|improve this answer
add comment

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

share|improve this answer
add comment

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/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.