且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSON:解析使用JQuery在HTML中显示

更新时间:2023-10-25 12:17:46

  $(文件)。就绪(函数(){
    $ .getJSON(HTTP://本地主机:1909年/ EN codeRS,功能(数据){

        $(#DIV-我的表)文本。(<表>);

        $每个(数据,功能(I,项目){
            $(#DIV-我的表)追加(< TR>< TD>中+ item.En coderName +< / TD>< TD>中+ item.En coderStatus +&所述; / TD>&所述; / TR>中);
        });

        $(#DIV-我的表)追加(< /表>)。

    });
});
 

I'm trying to parse data from a page of JSON in my ASP.NET MVC 1.0 web application. I need this data to display in a table on page load and I'm having a great deal of trouble mainly because I've never used JSON before. The JQuery site gives pretty terrible examples as well. This is what I have so far, I need help writing a function:

$("document").ready(function() {
        $.getJSON("http://localhost:1909/encoders",
            function(data) {
                $.each(data.items, function() {

                });
            });
    });

The URL above is currently just displaying JSON and the two columns I am getting from the SQL server to produce the JSON are EncoderName and EncoderStatus. The id is displayencoders.

Thanks!

edit: my controller looks like this:

[UrlRoute(Name = "GetEncoders", Path = "encoders")]
        public ActionResult GetEncoders() {
            var encoders = Database.GetEncoders();

            return Json(encoders);
        }

when compiled my /encoders/ looks like:

{

    * EncoderName: "rmcp2-encoder"
    * EncoderStatus: "inactive"

}

$("document").ready(function() {
    $.getJSON("http://localhost:1909/encoders", function(data) {

        $("#div-my-table").text("<table>");

        $.each(data, function(i, item) {
            $("#div-my-table").append("<tr><td>" + item.EncoderName +"</td><td>" + item.EncoderStatus + "</td></tr>");
        });

        $("#div-my-table").append("</table>");

    });
});