首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

ajax 获取JSON格式转换有关问题

2014-04-18 
ajax 获取JSON格式转换问题//这是我通过ajax webservice获取的JSON格式[{Name:Ajay Singh,Company:

ajax 获取JSON格式转换问题
//这是我通过ajax webservice获取的JSON格式

[{"Name":"Ajay Singh","Company":"Birlasoft Ltd.","Address":"LosAngeles California","Phone":"1204675","Country":"US"},{"Name":"Ajay Singh","Company":"Birlasoft Ltd.","Address":"D-195 Sector Noida","Phone":"1204675","Country":"India"}]


   $.ajax({
                url: "JSON.asmx/TestJSON",
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: "{}",
                success: function(json) {

             alert("返回的json格式");
                },
                error: function(x, e) {
                    alert(x.responseText);
                },  //============================
                complete: function(x) {
                    alert(x.responseText);
                }
            });

//请问我怎么把JSON转换为字符串赋值给文本框

[解决办法]
Json.Name 就可以取到Ajay Singh!
[解决办法]

for (var i = 0; i < json.d.length; i++) {
    alert(json.d[i].Name);
};

[解决办法]
因为webservice返回给你的是d,所以要用json.d
[解决办法]
用eval()呢?

success: function(json) {
        var jsondata = eval(json);
        alert(jsondata.Name);
        alert(jsondata.Company);
        ……
},

热点排行