0
{
    "subjectId": 1,
    "subjectName": "Physics",
    "topics": [
        {
            "id": 1,
            "name": "Thermodynamics",
            "children": null
        },
        {
            "id": 51,
            "name": "Random Variables",
            "children": [
                {
                    "id": 52,
                    "name": "Discrete random variable",
                    "children": [
                        {
                            "id": 53,
                            "name": "Probability mass function",
                            "children": null
                        }
                    ]
                }
            ]
        }
    ]
}

Please help me how to display the names from the above json data in below format through javascript and the json was generating dynamically and I want to run the loop until the children length equals to null.

<ul>
    <li>Physics
         <ul>
            <li>thermodynamics</li>
         </ul>                                  

    </li>
        </ul>
  • 1
    which technology you are working and share what you have tried. then if you got stucked with some exact issue, we will help you. Refer these links for asking proper questions (http://stackoverflow.com/help/how-to-ask) , (http://stackoverflow.com//help/mcve) and (sscce.org) – Panther Jul 09 '15 at 07:33

1 Answers1

0

You can use pre tag to show the code in the tree structure.

<pre>
{
"subjectId": 1,
"subjectName": "Physics",
"topics": [
    {
        "id": 1,
        "name": "Thermodynamics",
        "children": null
    },
    {
        "id": 2,
        "name": "Electrostatics",
        "children": null
    },
    {
        "id": 3,
        "name": "Sound and Music",
        "children": null
    },
    {
        "id": 51,
        "name": "Random Variables",
        "children": [
            {
                "id": 52,
                "name": "Discrete random variable",
                "children": [
                    {
                        "id": 53,
                        "name": "Probability mass function",
                        "children": null
                    }
                ]
            }
        ]
    }
]
}

Ankur
  • 730
  • 7
  • 28
  • I want to display the json in tree format through javascript – RadhaKrishna Jul 09 '15 at 08:40
  • 1
    First of all your question is not clear, can you please write in detail what is the requirement? How data is coming? Which technology you are using? – Ankur Jul 09 '15 at 09:43
  • try this may be this is answer you are looking for. http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript – Ankur Jul 09 '15 at 09:55
  • The requirement is to dislplay the data from the json file to unordered list in html. I'm using html and Java script json data is comming dynamically. – RadhaKrishna Jul 09 '15 at 10:39
  • The [link](http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript) which I have shared is not useful? – Ankur Jul 09 '15 at 19:25