javascript 如何使用多行/结果解析 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31019354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:07:33  来源:igfitidea点击:

How to parse JSON with multiple rows/results

javascriptjqueryajaxjson

提问by RossS

I don't know if this is malformed JSON string or not, but I cannot work out how to parse each result to get the data out.

我不知道这是否是格式错误的 JSON 字符串,但我无法弄清楚如何解析每个结果以获取数据。

This is the data.d response from my $.ajax function (it calls a WebMethod in Code Behind (C#)):

这是来自我的 $.ajax 函数的 data.d 响应(它在代码隐藏 (C#) 中调用 WebMethod):

{"Row":[
{"ID1":"TBU9THLCZS","Project":"1","ID2":"Y5468ASV73","URL":"http://blah1.com","Wave":"w1","StartDate":"18/06/2015 5:46:41 AM","EndDate":"18/06/2015 5:47:24 AM","Status":"1","Check":"0"},
{"ID1":"TBU9THLCZS","Project":"2","ID2":"T7J6SHZCH","URL":"http://blah2.com","Wave":"w1","StartDate":"23/06/2015 4:35:22 AM","EndDate":"","Status":"","Check":""}
]}

With all the examples I have looked at, only one or two showed something where my 'Row' is, and the solutions were not related, such as one person had a comma after the last array.

在我看过的所有示例中,只有一两个显示了我的“行”所在的位置,并且解决方案不相关,例如一个人在最后一个数组后有一个逗号。

I would be happy with some pointers if not even the straight out answer.

即使不是直接的答案,我也会对一些指示感到满意。

I have tried various combinations of response.Row, response[0], using $.each, but I just can't get it.

我已经尝试过使用 $.each 的 response.Row、response[0] 的各种组合,但我就是无法得到它。

EDIT, this is my ajax call:

编辑,这是我的ajax调用:

$.ajax({
    url: "Mgr.aspx/ShowActivity",
    type: "POST",
    data: JSON.stringify({
        "ID": "null"
    }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var data = response.hasOwnProperty("d") ? response.d : response;
        console.log(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        $('#lblResErr').html('<span style="color:red;">' + thrownError);
    }
});

At the moment I have just been trying to get ID1 value and ID2 value into the console.

目前我只是试图将 ID1 值和 ID2 值输入到控制台中。

EDIT (Resolved): Thanks to YTAM and Panagiotis !

编辑(已解决):感谢 YTAM 和 Panagiotis!

success: function (response) {
    var data = response.hasOwnProperty("d") ? response.d : response;
    data = JSON.parse(data);
    console.log(data);
}

Now the console is showing me an array of two objects, and now I know what to do with them !!

现在控制台向我展示了一个包含两个对象的数组,现在我知道如何处理它们了!!

回答by Jom George

First you have to parse the string with JSON.parse

首先你必须用 JSON.parse 解析字符串

var data= JSON.parse(rowData);

var data= JSON.parse(rowData);

Then you will get object like given below,

然后你会得到如下所示的对象,

data = {
"Row": [
{
  "ID1":"TBU9THLCZS",
  "Project":"1",
  "ID2":"Y5468ASV73",
  "URL":"http://blah1.com",
  "Wave":"w1",
  "StartDate":"18/06/2015 5:46:41 AM",
  "EndDate":"18/06/2015 5:47:24 AM",
  "Status":"1",
  "Check":"0"
},
{
  "ID1":"TBU9THLCZS",
  "Project":"2",
  "ID2":"T7J6SHZCH",
  "URL":"http://blah2.com",
  "Wave":"w1",
  "StartDate":"23/06/2015 4:35:22 AM",
  "EndDate":"",
  "Status":"",
  "Check":""
}
]}

Here I am giving two options either direct retrieve data from data variable or through loop.

在这里,我提供了两个选项,要么直接从数据变量中检索数据,要么通过循环。

data.row[0].ID1
data.row[0].Project
data.row[0].ID2

and so on

等等

OR

或者

use loop,

使用循环,

var result = json.row;
for (var i = 0; i < result.length; i++) {
  var object = result[i];
   for (property in object) {
    var value = object[property];
  }
}

Hope this helps.

希望这可以帮助。

回答by yohannes

you may be getting a json string from the web method rather than an actual JavaScriptobject. Parse it into a JavaScript object by doing

您可能会从 Web 方法而不是实际JavaScript对象中获取 json 字符串。通过执行将其解析为 JavaScript 对象

var data = JSON.parse(response); 

then you'll be able to iterate over data.Row

然后你就可以迭代了 data.Row