【问题标题】:Deserialize JSON with vb.net使用 vb.net 反序列化 JSON
【发布时间】:2018-05-11 19:30:02
【问题描述】:

这是我手动创建的类

Public Class ZohoList
    Public Property Select_Store() As String
        Get
            Return m_Select_Store
        End Get
        Set
            m_Select_Store = Value
        End Set
    End Property
    Private m_Select_Store As String
End Class

Public Class RootObject
    Public Property Zoho_List As List(Of ZohoList)
        Get
            Return m_Zoho_List
        End Get
        Set
            m_Zoho_List = Value
        End Set
    End Property
    Private m_Zoho_List As List(Of ZohoList)
End Class

在我得到这样的 JSON 响应后

{
   "Store_Money_Snapshot":[
      {
         "TODO":"YES",
         "Date_field":"10-May-2018",
         "Xpawn_Money":"3562",
         "Select_Store":"TEST",
         "Total_Counted_Money":"$ 3,000.00",
         "Store_from_Xpawn_pc2":"TEST",
         "Discrepancy_Amount":"$ -562.00",
         "Store_Problem_fixed":"NO",
         "ID":"1111111111111111111",
         "Image":"",
         "Store_Closing_Balance":"$ 33,482.00"
      },
      {
         "TODO":"YES",
         "Date_field":"10-May-2018",
         "Xpawn_Money":"10234",
         "Select_Store":"TEST2",
         "Total_Counted_Money":"$ 9,800.00",
         "Store_from_Xpawn_pc2":"TEST2",
         "Discrepancy_Amount":"$ -434.00",
         "Store_Problem_fixed":"NO",
         "ID":"2222222222222",
         "Image":"",
         "Store_Closing_Balance":"$ 33,482.00"
      }
   ]
}

我的反序列化对象的 vb.net 代码放在两行

Dim myO = JsonConvert.DeserializeObject(Of RootObject)(response)
            Dim items = myO.Zoho_List

            For Each item In items
                lTodo.Add(item.Select_Store.ToString)
                'Now comes th code
            Next

从整个响应中,我只需要 Select_Store 值,所以在课堂上我只放那个值

我也尝试将所有值放在我的班级中,但它仍然不会反序列化 JSON 响应

【问题讨论】:

    标签: json vb.net api zoho


    【解决方案1】:

    您的RootObject 与第一个花括号{ 配对。

    在 json 中的“根对象”上有一个属性:Store_Money_Snapshot,它不会出现在您的RootObject 中的任何位置。 Store_Money_Snapshot 是一个数组或 List<> 或对象。这些对象包含您的 Select_Store 属性。

    所以这样的事情应该会让你感动:

    Public Class RootObject
      '  RootObject is a HORRIBLE name.
    
        Public Property Store_Money_Snapshot As List(Of ZohoList)
    
    End Class
    
    Public Class ZohoList
      '  Again, ZohoList is a HORRIBLE name.
    
        Public Property Select_Store As String
    End Class
    

    我强烈建议您考虑使用更准确的描述性名称来命名您的类。

    【讨论】:

    • 我不敢相信它这么简单。我非常同意命名这些类。所以 ZohoList 类实际上是 - oneStore 和 RootObject 是 - Stores :)
    • 你明白了。尽情享受您的项目吧。
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2011-11-16
    相关资源
    最近更新 更多