首页 > 解决方案 > NewtonSoft JSON 将 JObject 添加到 JObject

问题描述

我目前有以下内容:

Dim persons_dr = dt_driverdetails.Select("[B@] = '2' and [PolRef@] = 'YOLX14PC01'")

Dim rss As JObject = New JObject(New JProperty("people"))

For Each row As DataRow In persons_dr

    Dim person As person = New person With {
    .person_id = row("person_id").ToString,
    .email = row("email").ToString,
    .mobile = row("mobile").ToString,
    .forename = row("forename").ToString,
    .surname = row("surname").ToString
    }

    Dim o As JObject = CType(JToken.FromObject(person), JObject)

rss.Add(New JProperty(o, New JObject()))

Next

数组中可以有多个person对象。 该循环为每个. 我想将它们全部添加到JObject 中,以便我的 JObject 最终类似于以下示例:people
For Eachpersonrss

{
    "people": [{
        "person_id": "71DLUOBNERUA1002",
        "email": "test1@gmail.com",
        "mobile": "+4412345678",
        "forename": "John",
        "surname": "Hopkins"

    },
    {
        "person_id": "71DLUOBNERUA1002",
        "email": "test1@gmail.com",
        "mobile": "+4412345678",
        "forename": "John",
        "surname": "Hopkins"
    }]
}

但是,我收到一个错误:Object cannot be convert to String
我认为这是因为我使用了 JProperty。

任何建议都会很棒——最好有人告诉我哪里出了问题,而不仅仅是纠正我的代码——渴望学习:)

更新

Public Class person
    Public Property person_id As String
    Public Property email As String
    Public Property mobile As String
    Public Property forename As String
    Public Property surname As String
End Class

Public Class people
    Public Property persons As List(Of person)
End Class

Private Sub create_person_json()

        Dim persons_dr = dt_driverdetails.Select("[B@] = '2' and [PolRef@] = 'YOLX14PC01'")
        Dim people As New people
        'Dim rss As JObject = New JObject(New JProperty("people"))

        For Each row As DataRow In persons_dr

            Dim person As person = New person With {
            .person_id = row("person_id").ToString,
            .email = row("email").ToString,
            .mobile = row("mobile").ToString,
            .forename = row("forename").ToString,
            .surname = row("surname").ToString
            }

            people.persons.Add(person)

            'Dim o As JObject = CType(JToken.FromObject(person), JObject)
        Next


    End Sub

更新 2

如果我想将另一组 JSON 嵌套到如下列表中:

    "people": [{
        "person_id": "71DLUOBNERUA1002",
        "email": "test1@gmail.com",
        "mobile": "+4412345678",
        "forename": "John",
        "surname": "Hopkins"
    "address": {
        "line_1": "76 This Place",
        "postal_code": "BB11 8DL",
        "country": "GB"
    },
}]

我可以做以下我的课程:

Public Class person
    Public Property person_id As String
    Public Property email As String
    Public Property mobile As String
    Public Property forename As String
    Public Property surname As String
    Public Property address As New address()
    Public Property dob As String
    'Public Property driverdetails As New driver_details()
End Class

Public Class address
    Public Property line_1 As String
    Public Property town_city As String
    Public Property postal_code As String
    Public Property country As String
End Class

那么我如何将它添加到我现有的代码中,因为我无法将它添加到“person”对象中,因为我需要先初始化类?

标签: jsonvb.netjson.net

解决方案


使用描述 JSON 的 .Net 类模型可以更简单地处理属性值和对象创建:它比使用 JToken 和派生对象要简单得多。

<JsonProperty()>属性允许在类模型中使用不同于 JSON 的属性名称。当 JSON 属性使用语言中的保留字名称或被认为有必要/首选时,通常会使用它。
Json 序列化器(或反序列化器)会将类模型的属性名称与指定的属性相匹配。

也可以看看:

问题中显示的 JSON 结构可以使用以下模型重现:
注意:此处的名称已更改以显示<JsonProperty()>属性的使用。

Public Class PeopleRoot
    <JsonProperty("people")>
    Public Property People As List(Of Person) = New List(Of Person)
End Class

Public Class Person
    <JsonProperty("person_id")>
    Public Property PersonId As String
    <JsonProperty("email")>
    Public Property Email As String
    <JsonProperty("mobile")>
    Public Property Mobile As String
    <JsonProperty("forename")>
    Public Property FirstName As String
    <JsonProperty("surname")>
    Public Property Surname As String
    <JsonProperty("address")>
    Public Property Address As PersonAddress
End Class

Public Class PersonAddress
    Public Property line_1 As String
    Public Property town_city As String
    Public Property postal_code As String
    Public Property country As String
End Class

要将PeopleRoot.PeopleProperty 初始化为List(Of Person),只需声明一个新PeopleRoot对象:

Dim root As New PeopleRoot()

' Add new Person objects:
root.People.AddRange({
    New Person With {
        .PersonId = "ID1",
        .Email = "email1@service1.com",
        .Mobile = "12345678",
        .FirstName = "Person1",
        .Surname = "Surname1",
        .Address = New PersonAddress() With {
            .line_1 = "Line1", .town_city = "Town1", .postal_code = "12345", .country = "Country1"
        }
    },
    New Person With {
        .PersonId = "ID2",
        .Email = "email2@service2.com",
        .Mobile = "34567890",
        .FirstName = "Person2",
        .Surname = "Surname2",
        .Address = New PersonAddress() With {
            .line_1 = "Line2", .town_city = "Town2", .postal_code = "34567", .country = "Country2"
        }
    }
})

' Serialize the new values
Dim json = JsonConvert.SerializeObject(root)

这个结构被序列化为原始的 JSON:

{
   "people":[
      {
         "person_id":"ID1",
         "email":"email1@service1.com",
         "mobile":"12345678",
         "forename":"Person1",
         "surname":"Surname1",
         "address":{
            "line_1":"Line1",
            "town_city":"Town1",
            "postal_code":"12345",
            "country":"Country1"
         }
      },
      {
         "person_id":"ID2",
         "email":"email2@service2.com",
         "mobile":"34567890",
         "forename":"Person2",
         "surname":"Surname2",
         "address":{
            "line_1":"Line2",
            "town_city":"Town2",
            "postal_code":"34567",
            "country":"Country2"
         }
      }
   ]
}

当然可以反序列化为:

Dim obJPeople = JsonConvert.DeserializeObject(Of PeopleRoot)(jsonString)

推荐阅读