【问题标题】:Receiving data in asp.net application在 asp.net 应用程序中接收数据
【发布时间】:2013-02-01 08:11:19
【问题描述】:

我向 api 发出 http 请求,该 api 作为回报向我的 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 应用程序接收和解析这些数据?

【问题讨论】:

    标签: asp.net json http


    【解决方案1】:

    要在 ASP.NET 中处理 JSON,您可以使用 Web.Script.Serialization.JavaScriptSerializer(添加对 System.Web.Extensions 的引用)。将.DeserializeObject() 返回的对象转换为Generic.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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2011-02-13
      • 2015-05-02
      相关资源
      最近更新 更多