如何设计对象模型以在序列化时生成特定的 JSON 字符串?



我需要将此 JSON 字符串发布到服务器:

{
"content-spec":"urn:spec://bosch.com/cindy/measurement-message#v2",
"device":{
"deviceID":"1de09010-ec04-11e7-bd8a-525400ed1935"
},
"measurements":[
{
"ts":"2017-07-25T15:40:00.000+02:00",
"series":{
"$_time":[
0
],
"BasY":[
1.5
]
}
}
]
}

我目前有以下结果:

{
"contentSpec": "urn:spec://bosch.com/cindy/measurement-message#v2",
"device": {
"deviceID": "1de09010-ec04-11e7-bd8a-525400ed1935"
},
"measurements": {
"ts": "2018-01-01T21:11:42.0519229+01:00",
"series": {
"$_time": 10212,
"value": 1.4
}
}
}

请注意,几个应该是数组的属性(在 JSON 中用 [] 标记)是简单对象。 如何使这些成为数组?

这是我的代码:

Public Function createJSONString(jContent As Dictionary(Of String, String)) As String
Dim tmpDate As DateTime = New DateTime(Now.Ticks, DateTimeKind.Utc.Local)
Dim timestamp As String = tmpDate.ToString("o") '2009-06-15T13:45:30.0000000Z
Dim makeJSON As JSONHandling = New JSONHandling
makeJSON.contentSpec = "urn:spec://bosch.com/cindy/measurement-message#v2"
makeJSON.device.deviceID = jContent("deviceID")
makeJSON.measurements.ts = timestamp
makeJSON.measurements.series.mtime = "10212"
makeJSON.measurements.series.value = 1.4

Return JsonConvert.SerializeObject(makeJSON).Replace("mtime", "$_time")
'"{""content-spec"":""urn:spec://bosch.com/cindy/measurement-message#v2"", ""device"":{ ""deviceID"":""" & jContent.Item(0) & """ }, ""measurements"":[{""ts"":""" & timestamp & """, ""series"":{""$_time"":[0], ""PickY"":[" & jContent.Item(3) & "]}}]}"
End Function

我的模型如下:

Public Class JSONHandling
Public contentSpec As String
Public device As JSONHandlingDevice = New JSONHandlingDevice
Public measurements As JSONHandlingMeasurements = New JSONHandlingMeasurements
End Class
Public Class JSONHandlingMeasurements
Public ts As String
Public series As JSONHandlingSeries = New JSONHandlingSeries
End Class
Public Class JSONHandlingSeries
Public mtime As Integer
Public value As Double
End Class

更新:当我想做一个以上的测量时,我必须做什么? 比如这样:

{
"content-spec": "urn:spec://bosch.com/cindy/measurement-message#v2",
"device": {
"deviceID": "1de09010-ec04-11e7-bd8a-525400ed1935"
},
"measurements": [
{
"ts": "2018-01-01T21:24:46.8354066+01:00",
"series": {
"$_time": [
0
],
"PickY": [
37.4
]
}
},
{
"ts": "2018-01-01T21:24:46.8354066+01:00",
"series": {
"$_time": [
0
],
"PickZ": [
92
]
}
},
{
"ts": "2018-01-01T21:24:46.8354066+01:00",
"series": {
"$_time": [
0
],
"PickC": [
0
]
}
}
]
}

以下类型可用于生成所需的 JSON:

Public Class JSONHandling
<JsonProperty("content-spec")> _
Public Property contentSpec As String
Public Property device As JSONHandlingDevice = New JSONHandlingDevice
Public Property measurements As List(Of JSONHandlingMeasurement) = New List(Of JSONHandlingMeasurement)()
End Class
Public Class JSONHandlingMeasurement
Public Property ts As String
Public Property series As JSONHandlingSeries = new JSONHandlingSeries()
End Class
Public Class JSONHandlingDevice
Public Property deviceID As String
End Class
Public Class JSONHandlingSeries
<JsonProperty("$_time")> _
Public Property TimeList As List(Of Integer) = new List(Of Integer)()
Public Property BasY As List(Of Double) = new List(Of Double)()
End Class

您可以按如下方式初始化它们:

Public Function createJSONString(jContent As Dictionary(Of String, String)) As String
Dim timestamp As String = "2017-07-25T15:40:00.000+02:00"
Dim makeJSON As JSONHandling = New JSONHandling With
{
.contentSpec = "urn:spec://bosch.com/cindy/measurement-message#v2",
.device = new JSONHandlingDevice With { .deviceID = jContent("deviceID") },
.measurements = New List(Of JSONHandlingMeasurement)() From 
{
new JSONHandlingMeasurement With
{
.ts = timestamp,
.series = new JSONHandlingSeries() With
{
.BasY = new List(Of Double)() From { 1.5 },
.TimeList = new List(Of Integer)() From { 0 }
}
}
}
}
Return JsonConvert.SerializeObject(makeJSON, Newtonsoft.Json.Formatting.Indented)
End Function

笔记:

  • "measurements"属性的值是一个 JSON数组- 一个以[(左括号)开头,以](右括号)结尾,其值用,(逗号)分隔的有序集合。 如 Json.NET 文档中所述,数组必须从 .Net 集合(如List(Of T)T()数组或实现项目类型TIEnumerable(Of T)的其他类型的数组进行序列化。 因此,例如,您的measurements财产需要成为List(Of JSONHandlingMeasurement)

  • 您有多个 JSON 属性("$_time""content-spec"),其名称无效 VB.NET 标识符。 在这种情况下,可以将<JsonProperty("Desired Name")>属性添加到成员,以覆盖序列化为 JSON 时使用的名称。

  • 我正在使用对象初始值设定项和集合初始值设定项语法初始化makeJSON

示例工作 .Net 小提琴。

相关内容

  • 没有找到相关文章

最新更新