Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse multidimensional JSON to html easily?

I want to parse json to html easily. I have a multidimensional json. so i want to parse this to html easily. any plugin or any simple code is available ? The following my json file.

    [
        {
            "country": "India",
            "state": [
                {
                    "name": "Delhi",
                    "capital": "New Delhi"
                },
                {
                    "name": "Tamilnadu",
                    "capital": "Chennai"
                }
            ]
        },
        {
            "country": "USA",
            "state": [
                {
                    "name": "Alabama",
                    "capital": "Montgomery"
                },
                {
                    "name": "Alaska",
                    "capital": "Juneau"
                }
            ]
}
]

The html is like this.

<ul>
        <li>India</li>
 <ul>
        <li>State1 :capital</li>
        <li>State2 :capita2</li>

</ul>
        <li>USA</li>
 <ul>
        <li>State1 :capital</li>
        <li>State2 :capita2</li>

</ul>

</ul>

I want to get output like this Which is best and simplest way to get this output?

like image 976
user3797053 Avatar asked Sep 25 '14 05:09

user3797053


People also ask

How fetch data from JSON to HTML?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.

Is JSON easy to parse?

JSON is a data interchange format that is easy to parse and generate. JSON is an extension of the syntax used to describe object data in JavaScript. Yet, it's not restricted to use with JavaScript. It has a text format that uses object and array structures for the portable representation of data.

What is JSONPath parse?

JSONPath is an expression language to parse JSON data. It's very similar to the XPath expression language to parse XML data. The idea is to parse the JSON data and get the value you want.

What is JSON manipulation?

This JSON Manipulation Package provides activities to manipulate and filter JSON text data. Overview. Overview. The JSON Manipulation Activity Package uses C# to manipulate and filter JSON text data.


2 Answers

You can use jquery jput plugin (http://plugins.jquery.com/jput/)

http://jsfiddle.net/mse255ko/1/

var data=[
        {
            "country": "India",
            "state": [
                {
                    "name": "Delhi",
                    "capital": "New Delhi"
                },
                {
                    "name": "Tamilnadu",
                    "capital": "Chennai"
                }
            ]
        },
        {
            "country": "USA",
            "state": [
                {
                    "name": "Alabama",
                    "capital": "Montgomery"
                },
                {
                    "name": "Alaska",
                    "capital": "Juneau"
                }
            ]
}
];

$(document).ready(function(){
	//loading json data initally 
	$('#maindiv').jPut({
		jsonData:data,
		name:'template1'
	});
});  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://shabeer-ali-m.github.io/jPut/js/jput.min.js"></script>
<div id="maindiv">
    <div jput="template1">
    <ul>
        <li>{{country}}</li>
        <ul>
            <li>State 1 Name : {{state.0.name}}, Capital : {{state.0.capital}}</li>
             <li>State 2 Name : {{state.1.name}}, Capital : {{state.1.capital}}</li>
        </ul>
    </ul>
    </div>    
</div>
like image 55
shabeer Avatar answered Nov 14 '22 07:11

shabeer


Please use jquery each for looping the JSON data, here during each iteration the variable html value is concatenated, so you can append to any DIV.

var data =    [
        {
            "country": "India",
            "state": [
                {
                    "name": "Delhi",
                    "capital": "New Delhi"
                },
                {
                    "name": "Tamilnadu",
                    "capital": "Chennai"
                }
            ]
        },
        {
            "country": "USA",
            "state": [
                {
                    "name": "Alabama",
                    "capital": "Montgomery"
                },
                {
                    "name": "Alaska",
                    "capital": "Juneau"
                }
            ]
}
];
jQuery(document).ready(function(){
                       
var html = "";
    jQuery.each(data, function(i,v){
      var temp = JSON.parse(JSON.stringify(v)); 
      html += "<ul><li>"+temp.country+"</li></ul>"; 
      jQuery.each(temp.state, function(j,val){ 
         html += "<li>" + val.name +"</li>"; 
      }); 
    });
    $("#result-div").html(html);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="result-div"></div>
like image 40
abdullacm Avatar answered Nov 14 '22 08:11

abdullacm