5

Sorry for my English. I have a Json Array :

"computers":
[
    {
        "id": 1,
        "type": "laptop",
        "manufactor":
        [
            {
                id: "m_1",
                name: "HP"
            },
            {
                id: "m_2",
                name: "Sony"
            }
        ] 
    },
    {
        "id": 2,
        "type": "desktop",
        "manufactor":
        [
            {
                id: "m_3",
                name: "Apple"   
            },
            {
                id: "m_4",
                name: "Tiger"   
            }
        ] 
    }
]

Could you tell me how can I do it like this: load computers data >> get 2 computer id 1 and 2 >> choose computer id = 1 >> load the manufactor of that computer. I can do it with mysql query but in json i don't know how to do that. Thanks for Suggestion!

Edit: I want to retrieve the manufacturer of the computer with ID - 1 from the JSON object above.

Thank to everyone for helping me out. I am going to create a page working with JSON look like DOJOX Rolling list, would you give me example or suggestion how to do like that ? thank you:D

1
  • Was your question answered? If so, please click the check mark next to the best answer to mark it as "accepted". Commented Nov 29, 2011 at 3:49

4 Answers 4

9

Using:

computers[0].manufactor[0].name

Will return "HP".

If you change it to:

computers[0].manufactor[1].name

You will get "Sony".

Changing it further to:

computers[1].manufactor[0].name

will return "Apple".

And finally:

computers[1].manufactor[1].name

returns "Tiger".

Look here for more information on how to manipulate JSON objects with Javascript.

Sign up to request clarification or add additional context in comments.

Comments

4

Say the given JSON data is in var data = {your give json string here}; and you want to access the computers array from data.

Array computers is accessed using

alert(data.computers);

For accessing the manufacturer, use the following:

for (var i in data.computers){
var id= i.id; /*returns id*/
var type = i.type;/*returns type*/
var manufactor = i.manufactor;/*returns munufactor i.e. an array*/
/*MANUFACTOR IS ARRAY SO YOU MAY USE*/
for (var j in manufactor){
    var m_name = j.name; /*returns manufactor's name*/
    var m_id = j.id; /*returns manufactor's id*/
}}

That is used if you have unknown number of computers and manufacturer in an array.

Comments

1

computer[1].id read more : http://www.json.org/fatfree.html

2 Comments

computer[1].id will only return the value of the id, ie 1 or 2. He wants the manufacturer so will have to go further into the object.
I know Kevin. What I gave was just a hint towards the answer, so he could learn on his own. :)
0
var jsonData = yourJsonData;
//Looking through computers
for(var i = 0; i<jsonData.computers.length; i++)
{
var id= jsonData.computers[i].id; // Get Id
var type = jsonData.computers[i].type; // Get Type
var manufacturers = jsonData.computers[i].manufactor; // Get manufacturers array
//Looking through manufacturer in computers
for (var j=0; j< manufacturers.length; j++ ){
   var manufacturer_id = manufacturers[j].id;   // Get manufacturer's Id
   var manufacturer_name = manufacturers[j].name;  // Get manufacturer's name
  }
}

Comments

Your Answer

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.