日期:2014-05-17  浏览次数:20482 次

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);
        ……
},