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()によって返されたオブジェクトをに変換して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
于 2013-02-08T22:06:08.497 に答える