简体   繁体   English

使用Jquery消耗json数据

[英]Consume json data using Jquery

I have a webMethod in my .cs page that returns a list. 我的.cs页面中有一个webMethod,它返回一个列表。

This returns json like this.. 这样会返回json。

{
    "d": [
        "Saint Clair",
        "Jefferson",
        "Shelby",
        "Tallapoosa",
        "Blount",
        "Talladega",
        "Marshall",
        "Cullman",
        "Bibb",
        "Walker",
        "Chilton",
        "Coosa",
        "Clay",
        "Tuscaloosa",
        "Hale",
        "Pickens",
        "Greene",
        "Sumter",
        "Winston"
    ]
}

and my jaquery method is 而我的jaquery方法是

   $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: StrUrl,
                data: "{'StateId':'" + Stateid + "'}",
                async: false,
                success: function (response) {
                    var obj = jQuery.parseJSON(response);
                    alert(obj.d[0]);

                }
            })

I want to loop through this json data in Jquery and get all the values in json data. 我想循环遍历Jquery中的json数据,并获取json数据中的所有值。

jQuery has a built in method called parseJSON() which accepts a string and returns an Javascript object. jQuery有一个名为parseJSON()的内置方法,该方法接受一个字符串并返回一个Javascript对象。

Here is a link to the documentation 这是文档的链接

Using .parseJSON() 使用.parseJSON()

var obj = jQuery.parseJSON('{"d":["Saint Clair","Jefferson","Shelby","Tallapoosa","Blount","Talladega","Marshall","Cullman","Bibb","Walker","Chilton","Coosa","Clay","Tuscaloosa","Hale","Pickens","Greene","Sumter","Winston"]}');
var p1 = obj.d;
for (var i = 0; i < p1.length; i++) {
    console.log(p1[i]);
}

Check this JSFiddle 检查这个JSFiddle

Updates: To check whether you're facing any error 更新:检查您是否遇到任何错误

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: StrUrl,
    data: "{'StateId':'" + Stateid + "'}",
    async: false,
    success: function (response) {
        var obj = jQuery.parseJSON(response);
        var p1 = obj.d;
        for (var i = 0; i < p1.length; i++) {
            console.log(p1[i]);
        }
    },    
       error: function(e){
            console.log("Error occured:" + e);
       }
});

I don't think you even need JQuery. 我认为您甚至不需要JQuery。

just give a call using jquery. 只是使用jquery打电话。 Now, here your D is array. 现在,这里的D是数组。 So, you need to go one step down to get the array. 因此,您需要向下移动以获得阵列。

Here is sample code 这是示例代码

var jsn = //is your json you are getting from web method

jsn.d.forEach(function(a){
console.log(a); // here you are getting every element.
});

Please let me know if any further details required. 请让我知道是否需要其他详细信息。

As per your comment it was not working. 根据您的评论,它不起作用。 here is working jsfiddle example. 这是工作的jsfiddle示例。 I have updated with little bit jquery to add things to dom. 我已经更新了一点jQuery来添加东西到dom。 So, you don't have to check console. 因此,您不必检查控制台。

Since you have an json response from the server, you can use dataType: 'json' in the ajax request. 由于您有来自服务器的json响应,因此可以在ajax请求中使用dataType: 'json' Then use $.each() to iterate through the array 然后使用$ .each()遍历数组

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: StrUrl,
    data: "{'StateId':'" + Stateid + "'}",
    async: false,
    dataType: 'json',
    success: function (response) {
        $.each(response, function(idx, value){
            alert(value)
        });
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM