首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用vb.net反序列化JSON

用vb.net反序列化JSON
EN

Stack Overflow用户
提问于 2018-05-11 19:30:02
回答 1查看 402关注 0票数 0

这是我手工创建的类。

代码语言:javascript
复制
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响应之后

代码语言:javascript
复制
{
   "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代码分为两行

代码语言:javascript
复制
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响应。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-11 19:41:37

你的RootObject和第一个卷曲大括号{成对。

然后json中的“根对象”有一个属性:Store_Money_Snapshot,它在RootObject中没有出现。Store_Money_Snapshot是一个数组或List<>或对象。这些对象包含您的Select_Store属性。

所以像这样的事情应该能让你动起来:

代码语言:javascript
复制
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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50299058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档