如何以更简单的方式即时创建JSON? [英] How can I create JSON on the fly in an easier way?
问题描述
我正在使用JsonTextWriter
构造一个JSON字符串,最终结果需要像这样.数据属性值"John Doe"将被替换为字符串变量,以在REST API调用中搜索不同的名称:
I am using JsonTextWriter
to construct a JSON string where the end result needs to look like this. The data property value "John Doe" will get replaced by a string variable to search for different names in a REST API call:
{
"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结构:
This JsonTextWriter
method is very hard for me to read, not intuitive. I'm guessing there is a way to instead create a class with all the properties and assign values? But I can't figure out how to deal with the nested stuff. Maybe better to use raw JSON for some parts to make it easier to make the code structure somewhat represent the final JSON? Here is the code I have now, it works, it is just clunky to read/edit. I'm thinking of something like LINQ to XML, where when you look at the LINQ code it is easy to "see" the XML structure :
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)
推荐答案
您可以定义对象初始值设定项语法 ,然后将其序列化:
You could define a hierarchy of anonymous objects corresponding to your JSON hierarchy using object initializer syntax, then serialize that:
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 此处.
如果您希望使用显式的命名类型,则可以使用代码生成工具,例如 https://jsonutils .com/或将JSON粘贴为类,以生成与您的JSON相对应的数据模型,如下所示:>
If you would prefer to use explicit, named types, you can use a code-generation tool such as https://jsonutils.com/ or Paste JSON as Classes to generate a data model corresponding to your JSON as follows:
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
然后使用相同的对象初始化程序语法分配和序列化:
Then allocate and serialize using the same object initializer syntax:
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
is a vb.net keyword, so I had to add brackets around the auto-generatedoperator
property.
如果要构建数据模型的更复杂的示例,则可能需要将自动生成的数组属性替换为List(Of T)
属性,例如
If you are going to be building more complex examples of your data model, you might want to replace the auto-generated array properties with List(Of T)
properties instead, e.g.
Public Property rules As List(Of Rule)
样本小提琴#2 此处.
这篇关于如何以更简单的方式即时创建JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!