【问题标题】:deserialization of json array response in asp.net vbasp.net vb中json数组响应的反序列化
【发布时间】:2026-01-15 11:35:01
【问题描述】:

有人可以帮我编写一个反序列化以下 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
          }
       }
    ]
}

这是新代码

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

公开课价格

     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>

【问题讨论】:

    标签: asp.net json vb.net


    【解决方案1】:

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

    这些是从您帖子中的 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.Net 这样的库,该库可用于反序列化并使用强类型类访问 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 页上出现另一条错误消息“关键字作为有效标识符无效”。突出显示“公共属性为字符串”我尝试将变量名称“to”更改为“to1”,但错误仍然存​​在。如上所示,我添加了完整的测试代码。请指教
    • 在这种情况下为有问题的属性分配 JsonProperty 属性 to 并将属性名称更改为不会与 vb 语法冲突的名称
    • 请问我该怎么做?编译器错误消息:BC30002:未定义类型“价格”。
    最近更新 更多