Heim > Backend-Entwicklung > PHP-Tutorial > 求如何样遍历json对象

求如何样遍历json对象

WBOY
Freigeben: 2016-06-13 13:38:50
Original
1296 Leute haben es durchsucht

求怎么样遍历json对象

HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    var jsonList ='[{"\u5168\u90e8":null,"\u52a8\u4f5c":"Action","\u7231\u60c5":"Romance","\u79d1\u5e7b":"Sci-Fi","\u707e\u96be":"Disaster","\u6050\u6016":"Horror","\u60ac\u7591":"Mystery","\u9b54\u5e7b":"Fantasy","\u6218\u4e89":"War","\u7f6a\u6848":"Crime","\u60ca\u609a":"Thriller","\u52a8\u753b":"Animation","\u4f26\u7406":"Ethics","\u7eaa\u5f55":"Documentary","\u5267\u60c5":"Drama"},{"\u5168\u90e8":false,"\u4e2d\u56fd\u5185\u5730":"8","\u53f0\u6e7e":"4","\u65e5\u672c":"12","\u97e9\u56fd":"1","\u7f8e\u56fd":"2","\u82f1\u56fd":"5","\u6cd5\u56fd":"13","\u897f\u73ed\u7259":"19","\u52a0\u62ff\u5927":"24","\u5fb7\u56fd":"6","\u6cf0\u56fd":"20","\u5370\u5ea6":"39","\u610f\u5927\u5229":"9","\u5176\u4ed6":"*"}]';
    

Nach dem Login kopieren


这个json 对象是后台一个数给生成的。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->

 [0] => Array
                (
                    [全部] => 
                    [动作] => Action
                    [爱情] => Romance
                    [科幻] => Sci-Fi
                    [灾难] => Disaster
                    [恐怖] => Horror
                    [悬疑] => Mystery
                    [魔幻] => Fantasy
                    [战争] => War
                    [罪案] => Crime
                    [惊悚] => Thriller
                    [动画] => Animation
                    [伦理] => Ethics
                    [纪录] => Documentary
                    [剧情] => Drama
                )

            [1] => Array
                (
                    [全部] => 
                    [中国内地] => 8
                    [台湾] => 4
                    [日本] => 12
                    [韩国] => 1
                    [美国] => 2
                    [英国] => 5
                    [法国] => 13
                    [西班牙] => 19
                    [加拿大] => 24
                    [德国] => 6
                    [泰国] => 20
                    [印度] => 39
                    [意大利] => 9
                    [其他] => *
                )


Nach dem Login kopieren


我主要是现实二个 select 控件动态加载后台传过来的数据内容。键值为select的名字, 值为select的 value

还请指点一下,我应该怎么做一上。

------解决方案--------------------
看样子你对 jq 也并不熟悉,拿了能用吗?
JScript code
var jsonList =[{"\u5168\u90e8":null,"\u52a8\u4f5c":"Action","\u7231\u60c5":"Romance","\u79d1\u5e7b":"Sci-Fi","\u707e\u96be":"Disaster","\u6050\u6016":"Horror","\u60ac\u7591":"Mystery","\u9b54\u5e7b":"Fantasy","\u6218\u4e89":"War","\u7f6a\u6848":"Crime","\u60ca\u609a":"Thriller","\u52a8\u753b":"Animation","\u4f26\u7406":"Ethics","\u7eaa\u5f55":"Documentary","\u5267\u60c5":"Drama"},{"\u5168\u90e8":false,"\u4e2d\u56fd\u5185\u5730":"8","\u53f0\u6e7e":"4","\u65e5\u672c":"12","\u97e9\u56fd":"1","\u7f8e\u56fd":"2","\u82f1\u56fd":"5","\u6cd5\u56fd":"13","\u897f\u73ed\u7259":"19","\u52a0\u62ff\u5927":"24","\u5fb7\u56fd":"6","\u6cf0\u56fd":"20","\u5370\u5ea6":"39","\u610f\u5927\u5229":"9","\u5176\u4ed6":"*"}];

$("<select id="selectTest0"></select>").appendTo("body");
$("<select id="selectTest1"></select>").appendTo("body");

$.each(jsonList, function(i, item){
  $.each(item, function(key, val) {
    $("<option value='"+val+"'>"+key+"</option>").appendTo($("#selectTest"+i))      
  });
}); <div class="clear">
                 
              
              
        
            </div>
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage