从jQuery调用的ASMX返回的嵌套JSON

时间:2016-11-15 22:09:22

标签: jquery json ajax datatables asmx

我需要填充HTML表客户端。为了做到这一点,我从jquery调用ASMX WebMethod。但是,我遇到了两个问题,我正在寻求帮助。

第一个问题:调用成功,在ASMX WebMethod中生成了干净的json。我在下面列出了一个剥离(为了便于阅读)json样本。但是,当我尝试在jquery中解析json时,它失败了。它以某种方式包装在XML包装器中:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">
...valid JSON data here...
</string>

我可以手动剥离包装,但我确定我必须做错事,因为它首先在那里。建议?

第二个问题:在我手动剥离包装器后,我可以解析JSON,但我无法弄清楚如何提取&#34;评论&#34;和&#34; statusTime&#34;数据嵌套在Report.ReportStatus中并将其发布到HTML表中。

$(document).ready(function () {
$.ajax({
    type: "POST",
    data: "{}",
    datatype: "json",
    url: 'WebService.asmx/GetINFO',
    contenttype: "application/json; charset=utf-8",
    success: ajaxSuccess,
    error: ajaxFailed
});
});
function ajaxSuccess(data, status) {
$('#myTable').DataTable({
    "ajax": data,
    "columns": [
        { "data": "Report.ReportStatus.statusTime" },
        { "data": "Report.ReportStatus.remark" }
    ]
});
}

ASMX WebService

[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetINFO()
{
    string url = "XXXX";
    string jsonStr;

    WebRequest request = (HttpWebRequest)WebRequest.Create(url);
    try
    {
        System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                jsonStr = reader.ReadToEnd();
            }
        }
        return jsonStr;
    }
    catch (WebException ex)
    {
    }
}
}

上面的WebMethod将jsonStr设置为等于以下JSON(为了便于阅读而删除)。我需要提取&#34;评论&#34;和&#34; statusTime&#34;数据嵌套在Report.ReportStatus中并将其发布到HTML表格。

{
"ActivityDetails": {
    "referralTime": null,
    "Activity": [{
        "function": "delay",
        "activityStartTime": "2016-09-26T20:36:00Z"
    }, {
        "function": "work",
        "activityStartTime": "2016-09-26T20:39:00Z"
    }],
    "totalDuration": "0014:26"
},
"Report": {
    "Information": {
        "Priority": "UNDEFINED"
    },
    "ReportStatus": [{
        "remark": "Blah",
        "statusTime": "2016-10-05T01:00:00Z"
    }, {
        "remark": "Blah Blah",
        "statusTime": "2016-10-04T15:10:00Z"
    }, {
        "statusTime": "2016-10-03T17:24:00Z",
        "remark": "Blah Blah Blah"
    }, {
        "remark": "Blah Blah Blah Blah",
        "statusTime": "2016-10-03T17:22:00Z"
    }, {
        "statusTime": "2016-09-26T20:00:00Z",
        "remark": "Opened 09/26/16 16:00"
    }]
},
"Response": {
    "description": "Success",
    "code": "0"
}
}  

1 个答案:

答案 0 :(得分:0)

我相信一旦ContentType设置为application/json,webservice就会返回一个没有包装器的有效JSON。例如:

Context.Response.ContentType = "application/json";

用于访问JSON数据。你试过JSON.Parse方法吗?这应该让你创建一个可以被索引以获得必要值的对象。在这种情况下,它会像,

data[0]Report[0].ReportStatus[0].statusTime