开发者

How to render an array of objects with JQuery

开发者 https://www.devze.com 2022-12-26 17:35 出处:网络
Assume the JSON returned is {\"2010143545\":{\"info\":[开发者_如何学C1,\"soccer\"]}, \"2010143547\":{\"info\":[0,\"Basketball\"]}

Assume the JSON returned is

{"2010143545":{"info":[开发者_如何学C1,"soccer"]},
 "2010143547":{"info":[0,"Basketball"]}
}

How do I use jQuery to render the array on ASP.NET page? More precisely, what kind of HTML template do I need to set to stuff the JSON with jQuery?

Thank you.


HTML for a result div:

<div id="myDiv">Results here</div>

And the JS. I'll try to make it clear how to use it.

$.get("webservice.asmx?id=555", function(data){
    var str = "";
    for(var i = 0; i < data.length; i++){
        var number = data[i];           // When i=0, this is "2010143545"
        var infoName = data[i].info[1]; // When i=0, this is "soccer"

        // Example of rendering
        str += "<p>The number " +number+ " holds the name " +infoName+ "</p>";
    }
    $("#myDiv").html(str);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消