Praneeth
Praneeth

Reputation: 178

Access JSON elements using Javascript/Jquery

I'm new to JSON, WebDevelopment, Javascript ...

I have the following JSON file

            {
                "component": "A",
                "status": 0,
                "children": [
                    {
                        "component": "AA",
                        "status": 0,
                        "children": [
                            {
                                "component": "AAA",
                                "status": 0,
                                "children": []
                            },
                            {
                                "component": "AAB",
                                "status": 0,
                                "children": []
                            }
                        ]
                    },
                    {
                        "component": "AB",
                        "status": 0,
                        "children": [
                            {
                                "component": "ABA",
                                "status": 0,
                                "children": []
                            },
                            {
                                "component": "ABB",
                                "status": 0,
                                "children": []
                            }
                        ]

                    }
                ]
            }

I need to read it using Javascript/Jquery and then display it. Later on I should develop code for Onclicking a "component A" on a webpage a list of components under it should be displayed and so on.Depending on the status variable a color is to be assigned to the component displayed ( in the form of an image like a box or something)

I wrote the following code:

            <html>
            <head>                   
            <title>Demo</title>
            </head>
            <body>

                <script src="jquery-1.9.1.min.js"></script>
                <script src="jquery-1.9.1.js"></script>
                <script>

                $(document).ready(function() {
                var myItems;

                $.getJSON('jsonfile.json', function(data) {

                 JSONArray children = jsonfile.getJSONArray("children");


                });
            });
                </script>
            </body>
            </html>                

Firstly what are the javascript equivalent of java functions for JSON like getJSONArray(), getJSONObject() etc Secondly any suggestions as to how i go about printing the details using what API's etc.

Upvotes: 1

Views: 1099

Answers (2)

webdeveloper
webdeveloper

Reputation: 17288

please show the syntax of $.each in this context

var result = $('#result');
$.each(obj.children, function(i, v){
    result.append('<div>' + i + ' - ' + v.component + '</div>');
});

Demo: http://jsfiddle.net/LbJBm/

Documentation: jQuery.each()

Upvotes: 1

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

var response            =   JSON.stringify(jsonvar);
response                =   JSON.parse(response);

response.componet will gives you A and thus you can access hte elements.

Upvotes: 0

Related Questions