使用jquery

时间:2015-04-22 13:45:34

标签: jquery html json html5

我有一个JSON,我需要所有节点的值。我尝试了它,但我只收到了第一和第二节点。第3个节点返回我[对象对象] 有人可以帮帮我吗?

我的JSON就像这样:

{
    1: {
        NAME: "NAME1",
        EXTERNALID: "idExternal1",
        LINK: "mylink1",
        DESCRIPTION: "Description1",
        PRODUCT_TOP: {
            PRICE: 549,
            NAME: "PRODUCT_TOP NAME",
            LONGTDESCRIPTION: "LongDesc1",
            LINK: "http://myurl.com",
            PRICESRC: "http://myurl.com",
            SHORTDESCRIPTION: "Tu hogar puede estar a la temperatura perfecta para ti, con el aire acondicionado Panasonic KIT YE12 QKE ¡Entra y descúbrelo! ",
            PHOTO: "http://myurl.com"
        }
        },
        2: {
            NAME: "NAME2",
            EXTERNALID: "idExternal2",
            LINK: "",
            DESCRIPTION: "Description2",
            PRODUCT_TOP: {
                PRICE: 13.99,
                NAME: "PRODUCT_TOP NAME",
                LONGTDESCRIPTION: "LongDesc2",
                LINK: "http://myurl.com",
                PRICESRC: "http://myurl.com",
                SHORTDESCRIPTION: "The Simpsons.",
                PHOTO: "http://myurl.com"
            }
        }
}                                                                                                                                                                                                                                                                                                                                                                                                   }                                                                                                                                                                                                                                                                                                                                                                                               }

我的jQuery脚本是:

$.getJSON( "myjsonurl.json", function( data ) {
    var items = [];
    $.each( data, function( i, val ) {
        items.push( "<li id='" + val + "'>" + i + ' / '+ val + "</li>" );
        $.each( val, function( x, val2 ) {
            items.push( "<li id='" + val2 + "'>" +x + ' / '+ val2 + "</li>" );
            $.each( val2, function( z, val3 ) {

                items.push( "<li id='" + val3 + "'>" +z + ' / '+ val3 + "</li>" );

            });
        });
    });

    $( "<ul/>", {
        "class": "my-new-list",
        html: items.join( "" )
    }).appendTo( ".send" );
});

有人帮我吗?

2 个答案:

答案 0 :(得分:2)

您获得&#34; [对象]的原因&#34;是因为您正在将对象转换为字符串。我会检查你的代码 - 但我想你在1级和2级你的id也是错误的。

这里有一些方法 - 但我会选择一个递归系统(没有重复的代码和无限深度)来检查对象的存在。试试这个尺寸:

http://jsfiddle.net/7ewokumg/

var finalItems = [];

function printProperties(o) {

    //Iterate through properties    
    $.each(o, function (i,v) {
        //If it's an object - recursively work with it
        if (typeof v === "object") {
            printProperties(v);
        } else {
            //It's just a string
            finalItems.push("<li id='" + v + "'>" + i + ' / '+ v + "</li>" );
        }
    });        
}

//process our data to populate finalItems
printProperties(data);

//Add all the items to an unordered list
$("<ul/>", {
    "class": "my-new-list",
     html: finalItems.join( "" )
    }).appendTo( "body" );

答案 1 :(得分:0)

您正在寻找展平并将您收到的JSON数据转换为字符串。这是对你接近它的一点抽象:

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="result"></div>
&#13;
std::vector
&#13;
&#13;
&#13;