【问题标题】:NewtonSoft JSON Add JObject to JObjectNewtonSoft JSON 将 JObject 添加到 JObject
【发布时间】:2021-02-22 10:12:36
【问题描述】:

我目前有以下:

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

people 数组中可以有多个 person 对象。
For Each 循环为每个 person 生成一个新的 JObject。我想将它们全部添加到rss JObject 中,这样我的 JObject 最终会像下面的示例:

{
    "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"
    }]
}

但是,我收到一个错误:无法将对象转换为字符串
我认为这是因为我使用了 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”对象中,我该如何将其添加到现有代码中?

【问题讨论】:

    标签: json vb.net json.net


    【解决方案1】:

    使用描述 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.People 属性初始化为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)
    

    【讨论】:

    • 使用上面的方法,如果值不存在并且我从 DataTable 中提取 DataRow 中的信息 - 我如何将其设置为“Nothing”,这样如果数据不存在存在它不会创建元素。我在 SerializeObject 设置中设置了 NullValueHandling.Ignore,如何将所有条目的默认值设置为“Nothing”,以便它们在序列化后不会出现。
    • 您需要发布另一个问题,添加您的操作细节。不清楚你在这里问什么。
    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2021-12-18
    相关资源
    最近更新 更多