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

时间:2018-01-01 20:15:52

标签: json vb.net json.net

我需要将此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
        ]
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:3)

以下类型可用于生成所需的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 docs中所述,必须从.Net集合(例如List(Of T)T()数组或其他类型实现IEnumerable(Of T)的类型{ {1}}。例如,您的T媒体资源需要成为measurements

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

  • 我正在使用object initializercollection initializer语法初始化"content-spec"

示例工作.Net fiddle