如何轻松地将多维JSON解析为html?

时间:2014-09-25 05:14:00

标签: jquery json multidimensional-array

我想轻松地将json解析为html。我有一个多维的json。所以我想轻松解析这个到HTML。任何插件或任何简单的代码可用吗? 以下是我的json文件。

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

html就是这样。

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

我想得到这样的输出 获得此输出的最佳和最简单的方法是什么?

5 个答案:

答案 0 :(得分:7)

您可以使用jquery jput插件(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>

答案 1 :(得分:5)

请使用jquery各自循环JSON数据,这里在每次迭代中连接变量html值,因此您可以附加到任何DIV。

&#13;
&#13;
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);
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="result-div"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:4)

1-使用forEach迭代数组 - 2使用object.hasOwnProperty检查字典中是否存在字符3-用$("< type-of-element >")创建html元素 4-以所需格式构建字符串。

你可以试试这个剧本:

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"
                }
            ]
    }
]

var to_append = $("body") //your append selector
if(data && data.length>0){
    var outer_ul = $("<ul>")
    data.forEach(function(e,i){
        if(e.hasOwnProperty("country")){
            outer_ul.append($("<li>", {text : e['country']}))
        }

        if(e.hasOwnProperty("state")){
            var inner_ul = $("<ul>")
            e['state'].forEach(function(__e,__i){
                if(__e.hasOwnProperty('capital')){
                    inner_ul.append($("<li>", {text : "State "+(__i+1) + " : " + __e['capital']}))
                }
            })
            outer_ul.append(inner_ul)
        }


    })
    to_append.append(outer_ul)
}

这将得到html输出:

<ul>
<li>India</li>
<ul><li>State 1 : New Delhi</li><li>State 2 : Chennai</li></ul>
<li>USA</li>
<ul><li>State 1 : Montgomery</li><li>State 2 : Juneau</li></ul>
</ul>

答案 3 :(得分:2)

这个问题的jQuery版本(实时样本):

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

var $root = $("ul");

$.each(countries, function () {
  $("<li>").appendTo($root).text(this.country);
  var $ul = $("<ul>").appendTo($("<li>").appendTo($root));
  $.each(this.state, function () {
    $("<li>").appendTo($ul).text(this.name + ": " + this.capital);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul></ul>

答案 4 :(得分:0)

当然你也可以使用AngularJS,它是角度的一个主要点,它很容易绑定数据。

&#13;
&#13;
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"
                }
            ]
}];

function bindData($scope)
{
    $scope.countries = data;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="" ng-controller="bindData">
<ul ng-repeat="c in countries">
	<li>{{c.country}}</li>
	<ul>
		<li ng-repeat="s in c.state">{{s.name}} :{{s.capital}}</li>
	</ul>
</ul>
</div>
&#13;
&#13;
&#13;