0

我向一个 api 发出一个 http 请求,它作为回报向我的 asp.net 应用程序发送一些响应。

发送的数据格式为

{
    "deliveryInfoNotification": {
        "deliveryInfo": {
            "address": "14075550100",
            "code": "0",
            "createdDateTime": "2011-05-12T00:55:25.313Z",
            "deliveryStatus": "DeliveredToNetwork",
            "direction": "outbound",
            "message": "Hello world",
            "messageId": "3e5caf2782cb2eb310878b03efec5083",
            "parts": "1",
            "senderAddress": "16575550100",
            "sentDateTime": "2011-05-12T00:55:34.359Z"
        }
    }
}

一旦它在那里接收,我如何通过我的 asp.net 应用程序接收和解析这些数据?

4

1 回答 1

0

要在 ASP.NET 中处理 JSON,您可以使用Web.Script.Serialization.JavaScriptSerializer(添加对 的引用System.Web.Extensions)。将返回的对象转换.DeserializeObject()为 aGeneric.Dictionary以使用它。

Dim json As String = String.Concat(
   "{",
       """deliveryInfoNotification"": {",
           """deliveryInfo"": {",
               """address"": ""14075550100"",",
               """code"": ""0"",",
               """createdDateTime"": ""2011-05-12T00:55:25.313Z"",",
               """deliveryStatus"": ""DeliveredToNetwork"",",
               """direction"": ""outbound"",",
               """message"": ""Hello world"",",
               """messageId"": ""3e5caf2782cb2eb310878b03efec5083"",",
               """parts"": ""1"",",
               """senderAddress"": ""16575550100"",",
               """sentDateTime"": ""2011-05-12T00:55:34.359Z""",
           "}",
       "}",
   "}")

Dim serializer As New Web.Script.Serialization.JavaScriptSerializer,
    jsonObject As System.Collections.Generic.Dictionary(Of String, Object) =
        DirectCast(serializer.DeserializeObject(json), System.Collections.Generic.Dictionary(Of String, Object))

If jsonObject.Count > 0 _
    AndAlso jsonObject.ContainsKey("deliveryInfoNotification") _
    AndAlso jsonObject("deliveryInfoNotification").ContainsKey("deliveryInfo") Then

    Dim deliveryInfo As System.Collections.Generic.Dictionary(Of String, Object) = jsonObject("deliveryInfoNotification")("deliveryInfo"),
        address As String = deliveryInfo("address"),
        code As String = deliveryInfo("code"),
        createdDateTime As String = deliveryInfo("createdDateTime"),
        deliveryStatus As String = deliveryInfo("deliveryStatus"),
        direction As String = deliveryInfo("direction"),
        message As String = deliveryInfo("message"),
        messageId As String = deliveryInfo("messageId"),
        parts As String = deliveryInfo("parts"),
        senderAddress As String = deliveryInfo("senderAddress"),
        sentDateTime As String = deliveryInfo("sentDateTime")

End If
于 2013-02-08T22:06:08.497 回答