1

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.

3
  • your json response is invalid. check here jsonlint.org and there is no JSONArray. you can use $.each to iterate the json object Commented Apr 1, 2013 at 12:29
  • Sorry I now validated it Can you please show the syntax of $.each in this context Commented Apr 1, 2013 at 12:34
  • jquery-1.9.1.min.js - is minimized jquery-1.9.1.js, remove one of them from your file Commented Apr 1, 2013 at 12:59

2 Answers 2

1

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()

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

3 Comments

Thanks a lot WebDeveloper I'll look into the code you gave and continue further from there
One more thing in the above code instead of declaring var obj=..... is a way to directly use the json file as object?
@Praneeth Yes, in your example it is data. Look at the docs: api.jquery.com/jQuery.getJSON
0
var response            =   JSON.stringify(jsonvar);
response                =   JSON.parse(response);

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

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.