我正在使用JsonTextWriter
构造一个JSON字符串,最终结果需要像这样。数据属性值“ John Doe”将被替换为字符串变量,以在REST API调用中搜索不同的名称:
{
"fields": [
"name",
"Company.name",
"email",
"mobile"
],
"query": {
"group": {
"operator": "AND",
"rules": [
{
"condition": "CONTAINS",
"moduleName": "Contact",
"field": {
"fieldName": "name"
},
"data": "John Doe"
}
]
}
}
}
这种JsonTextWriter
方法对我来说很难阅读,不直观。我猜想有一种方法可以代替创建具有所有属性的类并分配值?但是我不知道如何处理嵌套的东西。也许对某些部分使用原始JSON更好,以使代码结构更能代表最终JSON更容易?这是我现在拥有的代码,它可以正常工作,读取/编辑很笨拙。我在想类似LINQ to XML的东西,当您查看LINQ代码时,很容易“看到” XML结构:
Dim sb As StringBuilder = New StringBuilder()
Dim jw As JsonWriter = New JsonTextWriter(New StringWriter(sb))
jw.Formatting = Formatting.Indented
jw.WriteStartObject()
jw.WritePropertyName("fields")
jw.WriteStartArray()
jw.WriteValue("name")
jw.WriteValue("Company.name")
jw.WriteValue("email")
jw.WriteValue("mobile")
jw.WriteEndArray()
jw.WritePropertyName("query")
jw.WriteStartObject()
jw.WritePropertyName("group")
jw.WriteStartObject()
jw.WritePropertyName("operator")
jw.WriteValue("AND")
jw.WritePropertyName("rules")
jw.WriteStartArray()
jw.WriteStartObject()
jw.WritePropertyName("condition")
jw.WriteValue("CONTAINS")
jw.WritePropertyName("moduleName")
jw.WriteValue("Contact")
jw.WritePropertyName("field")
jw.WriteStartObject()
jw.WritePropertyName("fieldName")
jw.WriteValue("name")
jw.WriteEndObject()
jw.WritePropertyName("data")
jw.WriteValue("John Doe")
jw.WriteEndObject()
jw.WriteEndArray()
jw.WriteEndObject()
jw.WriteEndObject()
jw.WriteEndObject()
debug.writeline(sb.ToString)
答案 0 :(得分:0)
您可以使用anonymous objects定义与您的JSON层次结构相对应的object initializer syntax层次结构,然后对其进行序列化:
Dim data As String = "John Doe"
Dim example = New With { _
.fields = {"name","Company.name","email","mobile"}, _
.query = New With { _
.group = New With { _
.[operator] = "AND", _
.rules = { _
New With { _
.condition = "CONTAINS", _
.moduleName = "Contact", _
.field = new With { .fieldName = "name" }, _
.data = data _
} _
} _
} _
} _
}
Dim json as String = JsonConvert.SerializeObject(example, Formatting.Indented)
样本小提琴#1 here。
如果您希望使用显式的命名类型,则可以使用诸如https://jsonutils.com/或Paste JSON as Classes之类的代码生成工具来生成与JSON相对应的数据模型,如下所示:
Public Class Field
Public Property fieldName As String
End Class
Public Class Rule
Public Property condition As String
Public Property moduleName As String
Public Property field As Field
Public Property data As String
End Class
Public Class Group
' Note I had to fix this by adding brackets around "operator", since it's a keyword.
' Public Property operator As String
Public Property [operator] As String
Public Property rules As Rule()
End Class
Public Class Query
Public Property group As Group
End Class
Public Class Example
Public Property fields As String()
Public Property query As Query
End Class
然后使用相同的对象初始化程序语法分配和序列化:
Dim data As String = "John Doe"
Dim example = New Example With { _
.fields = {"name","Company.name","email","mobile"}, _
.query = New Query With { _
.group = New Group With { _
.[operator] = "AND", _
.rules = { _
New Rule With { _
.condition = "CONTAINS", _
.moduleName = "Contact", _
.field = new Field With { .fieldName = "name" }, _
.data = data _
} _
} _
} _
} _
}
Dim json as String = JsonConvert.SerializeObject(example, Formatting.Indented)
注意:
Operator
是vb.net关键字,因此我必须在自动生成的operator
属性周围添加括号。
如果您要构建数据模型的更复杂的示例,则可能想用List(Of T)
属性代替自动生成的数组属性,例如
Public Property rules As List(Of Rule)
样本小提琴#2 here。