我有一个 JSON
字符串,我正在尝试将其反序列化为 .Net 对象。
代码一直有效,直到遇到嵌套数组为止。然后它只返回空值。
在下面的示例中,当我调试并查看处理后的对象时,它只会有:
规则:
0: Will contain all data for the first node (project_number)
1: Will contain all data for the second node (agreement_number)
2: All fields will be Nothing
Condition: AND
Valid: true
如何反序列化整个对象?请注意,JSON
字符串来自一个库 (https://querybuilder.js.org/
),因此我对于如何创建该字符串犹豫不决。
这是我的代码:
Dim TestObj = JsonConvert.DeserializeObject(Of List(Of JsonObject))(TestString)
<Serializable()>
Public Class JsonObject
Public Property condition As String
Public Property Rules As List(Of Rules)
Public Property valid As Boolean
End Class
<Serializable()>
Public Class Rules
Public Property id As String
Public Property field As String
Public Property type As String
Public Property input As String
Public Property [operator] As String
Public Property value As String
End Class
Public Property TestString As String = "[
{
'condition':'AND',
'rules':[
{
'id':'project_number',
'field':'project_number',
'type':'string',
'input':'text',
'operator':'equal',
'value':'dfgdfs'
},
{
'id':'agreement_number',
'field':'agreement_number',
'type':'string',
'input':'text',
'operator':'contains',
'value':'asdfas'
},
{
'condition':'AND',
'rules':[
{
'id':'division',
'field':'division',
'type':'string',
'input':'select',
'operator':'in',
'value':[
'0',
'11719'
]
},
{
'condition':'AND',
'rules':[
{
'id':'ta',
'field':'ta',
'type':'string',
'input':'select',
'operator':'in',
'value':[
'24740',
'24744'
]
}
]
}
]
}
],
'valid':true
}]"
最佳答案
如上所述,您需要添加一个处理嵌套 Rules
对象的类。
在原始类结构中,Rules
类没有可以容纳第二层嵌套的 rules
属性。
但您还需要处理未确定的嵌套级别,因为其他嵌套类可以进一步嵌套 Rules
对象。
您可以添加一个新类,通过引用自身来处理这种嵌套:
Partial Public Class RulesList
Public Id As String
Public Value As Long()
'(...)
Public Rules As RulesList()
End Class
此外,它必须包含 Value
对象的数组/列表。
请注意,我使用了数组而不是列表:检查结果更容易,您可以使用List(Of [Type ])
如果您愿意,最终输出不会改变。
这个示例类(名为Queries
)实现了这种结构。
它还包括作为静态
(共享
)方法的序列化和反序列化(简化,无错误检查/处理)。
假设 jsonInput
是您的 JSON 对象:
反序列化为List(Of QueryBuilder)
:
Dim myQueries = Queries.Deserialize(jsonInput)
将 List(Of QueryBuilder)
序列化回原始 JSON 对象。
请注意,使用 Visual Studio 中的 JSON 可视化工具,结构将与原始结构完全相同:
Dim jsonOutput = Queries.Serialize(myQueries)
查询
类:
Imports Newtonsoft.Json
Public Class Queries
Public Class QueryBuilder
Public Condition As String
Public Rules As Rules()
Public Valid As Boolean
End Class
Public Class Rules
Public Id As String
Public Field As String
<JsonProperty("type")>
Public QueryType As String
Public Input As String
<JsonProperty("operator")>
Public QueryOperator As String
Public Value As String
Public Condition As String
Public Rules As RulesList()
End Class
Partial Public Class RulesList
Public Id As String
Public Field As String
<JsonProperty("type")>
Public QueryType As String
Public Input As String
<JsonProperty("operator")>
Public QueryOperator As String
Public Value As Long()
Public Condition As String
Public Rules As RulesList()
End Class
Public Shared Function Deserialize(jsonSource As String) As List(Of QueryBuilder)
Return JsonConvert.DeserializeObject(Of List(Of QueryBuilder))(jsonSource)
End Function
Public Shared Function Serialize(classObject As List(Of QueryBuilder)) As String
Return JsonConvert.SerializeObject(classObject)
End Function
End Class
关于asp.net - 反序列化 JSON 嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55974604/