Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get value through JSON array like query

Tags:

json

html

jquery

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

like image 545
user1053120 Avatar asked Nov 18 '11 05:11

user1053120


1 Answers

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.

like image 188
Kevin Anthony Oppegaard Rose Avatar answered Nov 14 '22 03:11

Kevin Anthony Oppegaard Rose