asp.net vb中json数组响应的反序列化

asp.net vb中json数组响应的反序列化,asp.net,json,vb.net,Asp.net,Json,Vb.net,有人能帮我写一个脚本来反序列化下面的json响应吗。我想访问包含的变量及其值 { "results":[ { "bulkId":"80664c0c-e1ca-414d-806a-5caf146463df", "messageId":"bcfb828b-7df9-4e7b-8715-f34f5c61271a", "to":"41793026731", "sentAt":"2015-02-12T09:51:43.123+0100",

有人能帮我写一个脚本来反序列化下面的json响应吗。我想访问包含的变量及其值

{  
  "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
          }
       }
    ]
}
这是新代码


''' 
''JSON序列化和反序列化助手类
''' 
公共类价格

     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>
Public Property pricePerMessage为双精度
作为字符串的公共属性货币
末级
公共阶级地位
作为整数的公共属性groupId
作为字符串的公共属性groupName
作为整数的公共属性id
作为字符串的公共属性名称
公共属性描述为字符串
末级
公共类错误模型
作为整数的公共属性groupId
作为字符串的公共属性groupName
作为整数的公共属性id
作为字符串的公共属性名称
公共属性描述为字符串
永久性公共财产
末级
公开课成绩
作为字符串的公共属性bulkId
作为字符串的公共属性messageId
将公共属性设置为字符串
公共财产sentAt As DateTime
公共财产不作为日期时间
公共属性smsCount为整数
公共属性mccMnc作为字符串
公屋价格
公共属性回调数据为字符串
公共财产地位
作为错误模型的公共属性错误
末级
公众课堂反应
公共财产结果结果()
末级
受保护的子页加载(发件人作为对象,e作为事件参数)
Dim json=“{”“results”“:[{”“bulkId”“:”“1454508683222745512”“messageId”“:”“fbaa8cbd-62a2-4cdd-92a3-ebc962586356”“to”“:”“234816734353”“sentAt”“:”“2016-02-03T14:11:24.509+0000”“doneAt”“:”“2016-02-05T14:11:30.017+0000”“smsCount”“:1”“price”“:{”“pricePerMessage”“:1.2500000000”“货币:”“groupId”“,”“状态:”{“groupName”“:”“EXPIRED”“”“id”“:15”“name”“:”“EXPIRED\u EXPIRED”“”“description”“:”“Message EXPIRED”“}”“error”“:{”“groupId”“:1”“groupName”“:”“HANDSET\u ERRORS”“,”“id”“:27”“name”“:”“EC\u缺席订户”“description”“:”“缺席订户”“永久订户”“:false}]}”
Dim response As response=JsonConvert.DeserializeObject(响应的类型)(json);
Dim bulkId作为字符串=response.Results(0).bulkId
响应写入(bulkId)
端接头
无标题文件

您可以使用类似的服务来生成反序列化所需的类

这些类是从您文章中的JSON生成的

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
有了这些类,您就可以使用一个类似的库,它可以用来使用强类型类反序列化和访问JSON的属性

假设数据存储在变量
json

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

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

您可以使用类似的服务来生成反序列化所需的类

这些类是从您文章中的JSON生成的

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
有了这些类,您就可以使用一个类似的库,它可以用来使用强类型类反序列化和访问JSON的属性

假设数据存储在变量
json

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

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

感谢您的回复。它出现了以下错误:突出显示第35行。编译器错误消息:BC30183:关键字作为标识符无效。源错误:第33行:结束类第34行:第35行:公共类错误看起来与保留关键字有命名冲突。我更新了类和属性。查看它现在是否有效。是的。谢谢。它来了第46页出现另一个错误消息“关键字作为有效标识符无效”。突出显示“publicproperty to as String”我试图将变量名改为“to”改为1“但错误依然存在。我已经添加了完整的测试代码,如上所示。请建议在有问题的属性上指定一个JsonProperty属性,在这种情况下,将
指定给
,并将该属性的名称更改为不会与vb语法冲突的名称。我该怎么做?编译器错误消息:BC30002:未定义类型“Price”。感谢您的响应。出现以下错误:突出显示第35行。编译器错误消息:BC30183:关键字作为标识符无效。源错误:第33行:结束类第34行:第35行:公共类错误与保留关键字的命名冲突。我更新了类和属性。看看现在能不能用是的。谢谢它在第46页出现另一个错误消息“关键字作为有效标识符无效”。突出显示“publicproperty to As String”我试图将变量名改为“to1”,但错误仍然存在。我已经添加了完整的测试代码,如上所示。请建议在有问题的属性上指定一个JsonProperty属性,在这种情况下,将
指定给
,并将该属性的名称更改为不会与vb语法冲突的名称。我该怎么做?编译器错误消息:BC30002:未定义类型“Price”。