【问题标题】:Adding to a JSON message in .NET在 .NET 中添加到 JSON 消息
【发布时间】:2018-12-23 12:16:35
【问题描述】:

我希望能够下载 JSON 文件,添加到其中,然后将修改后的文件保存回 Web 服务器。 JSON 文件如下所示:

  {
  "vehicles": {
        "motorbike": {
            "example": [{
                    "make": "Kawasaki",
                    "model": "Z1"
                },
                {
                    "make": "Honda",
                    "model": "Goldwing"
                }
            ],
            "description": "Usually 2 wheels, with no roof"
        },
        "car": {
            "example": [{
                "make": "Skoda",
                "model": "Fabia"
            }],
            "description": "Usually 4 wheels, with at least 4 seats and roof"
        },
        "lorry": {
            "example": [{
                "make": "Mack",
                "model": "Anthem"
            }],
            "description": "Usually lots of wheels, with a roof, and a loud horn"
        }
    }
}

在 MSDN 上的 thread 中有一种提示如何添加到现有 JSON 消息,但该示例是针对 JSON 消息的,其结构比我的简单得多。在下面的示例中,我想将另一辆车添加到 JSON 消息中。我可以修改现有的汽车,并创建一个带有新车的新对象(有点),但不知道如何将结果合并回我现有的 JSON:

Imports System.Web.Script.Serialization

Public Class Example
    Public Property make As String
    Public Property model As String
End Class

Public Class Motorbike
    Public Property example As Example()
    Public Property description As String
End Class

Public Class Car
    Public Property example As Example()
    Public Property description As String
End Class

Public Class Lorry
    Public Property example As Example()
    Public Property description As String
End Class

Public Class Vehicles
    Public Property motorbike As Motorbike
    Public Property car As Car
    Public Property lorry As Lorry
End Class

Public Class VehicleRoot
    Public Property vehicles As Vehicles
End Class
Dim contents As String

'Deserialise the JSON
Using streamReader As StreamReader = New StreamReader("C:\Users\idiot\Desktop\example.json")
    'Get entire contents of file in string.
    contents = streamReader.ReadToEnd()
End Using

Dim CarObject As VehicleRoot = New JavaScriptSerializer().Deserialize(Of VehicleRoot)(contents)

'Can amend existing JSON, but how to add?
CarObject.vehicles.car.example(0).model = "Octavia"

Dim newData As New List(Of Example)

With newData
    .Add(New Example With {.make = "Bugatti", .model = "Veyron"})
End With

Dim p As New Car With {.example = newData.ToArray}
'I'm stuck now.
'CarObject.vehicles.concat

'Reserialise the amended JSON
Dim serializer = New JavaScriptSerializer()
Dim serializedResult = serializer.Serialize(p)

Using writer As StreamWriter =
    New StreamWriter("C:\Users\idiot\Desktop\example_v2.json")

    writer.Write(serializedResult)
End Using

我正在使用 .NET JavaScriptSerializer。我查看了 JSON.net 中的代码示例,以防有任何我可以/应该使用的东西,但实际上看不到任何东西。

任何帮助,或指出我正确的方向,(VB 或 C#)都非常感激。

【问题讨论】:

    标签: c# .net json vb.net


    【解决方案1】:

    如果您只是添加一个项目,而不是创建一个单独的列表,您可以只使用 ResizeReDim Preserve 您现有的数组:

    Array.Resize:

    Array.Resize(CarObject.vehicles.car.example, CarObject.vehicles.car.example.Length + 1)
    

    ReDim Preserve:

    ReDim Preserve CarObject.vehicles.car.example(CarObject.vehicles.car.example.Length)
    

    然后在空数组槽中添加一辆新车:

    CarObject.vehicles.car.example(CarObject.vehicles.car.example.Length - 1) = New Example With {
        .make = "Bugatti",
        .model = "Veyron"
    }
    

    如果您确实需要一次添加多个项目,请再次调整数组大小并使用For-loop 将列表中的项目放入数组中:

    Dim newData As New List(Of Example)
    
    With newData
        .Add(New Example With {.make = "Bugatti", .model = "Veyron"})
        .Add(New Example With {.make = "Ford", .model = "Focus"})
    End With
    

    Array.Resize:

    Array.Resize(CarObject.vehicles.car.example, CarObject.vehicles.car.example.Length + newData.Count)
    

    ReDim Preserve:

    ReDim Preserve CarObject.vehicles.car.example(CarObject.vehicles.car.example.Length + newData.Count - 1)
    

    然后循环:

    For i = 0 To newData.Count - 1
        Dim j As Integer = CarObject.vehicles.car.example.Length - newData.Count + i
        CarObject.vehicles.car.example(j) = newData(i)
    Next
    

    【讨论】:

    • 完美运行!非常感谢!上帝朱尔!
    • @basinbasin :很高兴我能帮上忙!上帝 Jul och Gott Nytt År! ;)
    猜你喜欢
    • 1970-01-01
    • 2017-10-17
    • 2023-03-03
    • 2014-02-23
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多