1
votes

Can someone please help me with a script to deserialize the below json response. I will like to access the contained variables and their values

{  
  "results":[  
  {  
     "bulkId":"80664c0c-e1ca-414d-806a-5caf146463df",
     "messageId":"bcfb828b-7df9-4e7b-8715-f34f5c61271a",
     "to":"41793026731",
     "sentAt":"2015-02-12T09:51:43.123+0100",
     "doneAt":"2015-02-12T09:51:43.127+0100",
     "smsCount":1,
     "mccMnc": "22801",
     "price":{  
        "pricePerMessage":0.01,
        "currency":"EUR"
     },
     "callbackData": "User defined data.",
     "status":{  
        "groupId":3,
        "groupName":"DELIVERED",
        "id":5,
        "name":"DELIVERED_TO_HANDSET",
        "description":"Message delivered to handset"
     },
     "error":{  
        "groupId":0,
        "groupName":"OK",
        "id":0,
        "name":"NO_ERROR",
        "description":"No Error",
        "permanent":false
     }
  },
  {  
     "bulkId":"08fe4407-c48f-4d4b-a2f4-9ff583c985b8",
     "messageId":"12db39c3-7822-4e72-a3ec-c87442c0ffc5",
     "to":"41793026727",
     "sentAt":"2015-02-12T09:50:22.221+0100",
     "doneAt":"2015-02-12T09:50:22.232+0100",
     "smsCount":1,
     "mccMnc": "22801",
     "price":{  
        "pricePerMessage":0.01,
        "currency":"EUR"
     },
     "callbackData": "reset_password",
     "status":{  
        "groupId":3,
        "groupName":"DELIVERED",
        "id":5,
        "name":"DELIVERED_TO_HANDSET",
        "description":"Message delivered to handset"
     },
     "error":{  
        "groupId":0,
        "groupName":"OK",
        "id":0,
        "name":"NO_ERROR",
        "description":"No Error",
             "permanent":false
          }
       }
    ]
}

This is the new code <%@ Page Language="VB" Debug = "true"%> <%@ Import Namespace="System.Data.SqlClient"%> <%@ Import Namespace="System.net.mail"%> <%@ Import Namespace="System.Data"%> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.Net.http" %> <%@ Import Namespace= "System.Web" %> <%@ Import Namespace ="System.Runtime.Serialization.Json" %> <%@ Import Namespace="System.text" %> <%@ Import Namespace="System.IO" %> <%@ Import namespace="System.Web.Script.Serialization" %> <%@ Import Namespace="RestSharp" %> <%@ Import namespace= "System.Collections.Generic" %> <%@ Import namespace ="System.Linq" %> <%@ Import namespace ="Newtonsoft.Json.Linq" %>

 <SCRIPT language="vb" runat="server">
 ''' <summary>
 ''' JSON Serialization and Deserialization Assistant Class
 ''' </summary>

Public Class Price

     Public Property pricePerMessage As Double
     Public Property currency As String
 End Class

 Public Class Status
     Public Property groupId As Integer
     Public Property groupName As String
     Public Property id As Integer
     Public Property name As String
     Public Property description As String
 End Class

 Public Class ErrorModel
     Public Property groupId As Integer
     Public Property groupName As String
     Public Property id As Integer
     Public Property name As String
     Public Property description As String
     Public Property permanent As Boolean
 End Class

 Public Class Result
     Public Property bulkId As String
     Public Property messageId As String
     Public Property to As String
     Public Property sentAt As DateTime
     Public Property doneAt As DateTime
     Public Property smsCount As Integer
     Public Property mccMnc As String
     Public Property price As Price
     Public Property callbackData As String
     Public Property status As Status
     Public Property error As ErrorModel
 End Class

 Public Class Response
  Public Property results As Result()
 End Class

 Protected Sub Page_Load(sender As Object, e As EventArgs)
 Dim json  = "{""results"":   [{""bulkId"":""1454508683222745512"",""messageId"":""fbaa8cbd-62a2-4cdd-92a3-ebc962586356"",""to"":""2348166734353"",""sentAt"":""2016-02-03T14:11:24.509+0000"",""doneAt"":""2016-02-05T14:11:30.017+0000"",""smsCount"":1,""price"":{""pricePerMessage"":1.2500000000,""currency"":""NGN""},""status"":{""groupId"":4,""groupName"":""EXPIRED"",""id"":15,""name"":""EXPIRED_EXPIRED"",""description"":""Message expired""},""error"":{""groupId"":1,""groupName"":""HANDSET_ERRORS"",""id"":27,""name"":""EC_ABSENT_SUBSCRIBER"",""description"":""Absent Subscriber"",""permanent"":false}}]}"

  Dim response As Response = JsonConvert.DeserializeObject(Of Response)(json);

 Dim bulkId As String = response.Results(0).bulkId
    Response.Write(bulkId)
 End Sub
 </SCRIPT>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <title>Untitled Document</title>
 </head>
 <body>
 <form runat="server">

 </form>


 </body>
 </html>
1

1 Answers

0
votes

You can use a service like this to generate the classes you would need for the deserialization.

These are the classes that were generated from the JSON in your post

Public Class Price
    Public Property pricePerMessage As Double
    Public Property currency As String
End Class

Public Class Status
    Public Property groupId As Integer
    Public Property groupName As String
    Public Property id As Integer
    Public Property name As String
    Public Property description As String
End Class

Public Class ErrorModel
    Public Property groupId As Integer
    Public Property groupName As String
    Public Property id As Integer
    Public Property name As String
    Public Property description As String
    Public Property permanent As Boolean
End Class

Public Class Result
    Public Property bulkId As String
    Public Property messageId As String
    Public Property to As String
    Public Property sentAt As DateTime
    Public Property doneAt As DateTime
    Public Property smsCount As Integer
    Public Property mccMnc As String
    Public Property price As Price
    Public Property callbackData As String
    Public Property status As Status
    Public Property error As ErrorModel
End Class

Public Class Response
    Public Property results As Result()
End Class

With the classes you can then use a library like Json.Net which can be used to deserialize and access the properties of the JSON using strongly typed classes.

assuming the data is stored in a variable json

Dim response As Response = JsonConvert.DeserializeObject(Of Response)(json);

Dim bulkId As String = response.Results(0).bulkId