如何获取JSON键和值?

时间:2022-10-26 11:14:56

I have written following code to get JSON result from webservice.

我已经编写了以下代码以从webservice获得JSON结果。

function SaveUploadedDataInDB(fileName) {
            $.ajax({
                type: "POST",
                url: "SaveData.asmx/SaveFileData",
                data: "{'FileName':'" + fileName + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var result = jQuery.parseJSON(response.d);
                    //I would like to print KEY and VALUE here.. for example
                    console.log(key+ ':' + value)
                    //Addess : D-14 and so on..
                   }
            });
        }

Here is response from webservice: 如何获取JSON键和值?

以下是来自webservice的回应:

Please help me to print Key and it's Value

请帮我打印键和它的值。

3 个解决方案

#1


89  

It looks like you're getting back an array. If it's always going to consist of just one element, you could do this (yes, it's pretty much the same thing as Tomalak's answer):

看起来你得到了一个数组。如果它总是由一个元素组成,你可以这么做(是的,这和托玛拉克的答案差不多):

$.each(result[0], function(key, value){
    console.log(key, value);
});

If you might have more than one element and you'd like to iterate over them all, you could nest $.each():

如果您可能有多个元素,并且希望对它们进行迭代,您可以嵌套$.each():

$.each(result, function(key, value){
    $.each(value, function(key, value){
        console.log(key, value);
    });
});

#2


11  

$.each(result, function(key, value) {
  console.log(key+ ':' + value);
});

#3


10  

First, I see you're using an explicit $.parseJSON(). If that's because you're manually serializing JSON on the server-side, don't. ASP.NET will automatically JSON-serialize your method's return value and jQuery will automatically deserialize it for you too.

首先,我看到您使用的是显式的$. parsejson()。如果这是因为您在服务器端手动序列化JSON,请不要这样做。ASP。NET将自动JSON-serialize您的方法的返回值,jQuery也将自动反序列化您的方法。

To iterate through the first item in the array you've got there, use code like this:

要遍历数组中的第一个条目,请使用如下代码:

var firstItem = response.d[0];

for(key in firstItem) {
  console.log(key + ':' + firstItem[key]);
}

If there's more than one item (it's hard to tell from that screenshot), then you can loop over response.d and then use this code inside that outer loop.

如果有多个项目(从屏幕截图中很难看出),那么您可以循环响应。然后在外部循环中使用这个代码。

#1


89  

It looks like you're getting back an array. If it's always going to consist of just one element, you could do this (yes, it's pretty much the same thing as Tomalak's answer):

看起来你得到了一个数组。如果它总是由一个元素组成,你可以这么做(是的,这和托玛拉克的答案差不多):

$.each(result[0], function(key, value){
    console.log(key, value);
});

If you might have more than one element and you'd like to iterate over them all, you could nest $.each():

如果您可能有多个元素,并且希望对它们进行迭代,您可以嵌套$.each():

$.each(result, function(key, value){
    $.each(value, function(key, value){
        console.log(key, value);
    });
});

#2


11  

$.each(result, function(key, value) {
  console.log(key+ ':' + value);
});

#3


10  

First, I see you're using an explicit $.parseJSON(). If that's because you're manually serializing JSON on the server-side, don't. ASP.NET will automatically JSON-serialize your method's return value and jQuery will automatically deserialize it for you too.

首先,我看到您使用的是显式的$. parsejson()。如果这是因为您在服务器端手动序列化JSON,请不要这样做。ASP。NET将自动JSON-serialize您的方法的返回值,jQuery也将自动反序列化您的方法。

To iterate through the first item in the array you've got there, use code like this:

要遍历数组中的第一个条目,请使用如下代码:

var firstItem = response.d[0];

for(key in firstItem) {
  console.log(key + ':' + firstItem[key]);
}

If there's more than one item (it's hard to tell from that screenshot), then you can loop over response.d and then use this code inside that outer loop.

如果有多个项目(从屏幕截图中很难看出),那么您可以循环响应。然后在外部循环中使用这个代码。