C# 添加到.NET中的JSON消息

C# 添加到.NET中的JSON消息,c#,.net,json,vb.net,C#,.net,Json,Vb.net,我希望能够下载一个JSON文件,添加到其中,然后将修改后的文件保存回web服务器。JSON文件如下所示: { "vehicles": { "motorbike": { "example": [{ "make": "Kawasaki", "model": "Z1" }, {

我希望能够下载一个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上的这篇文章中,有一个如何添加到现有JSON消息的提示,但是这个例子是针对JSON消息的,它的结构比我的要简单得多。在下面的示例中,我想在JSON消息中添加另一辆车。我可以修改一辆现有的汽车,并创建一个新对象,其中包含一辆新车(某种程度上),但不知道如何将结果合并回我现有的JSON:

导入System.Web.Script.Serialization
公开课范例
公共属性设置为字符串
作为字符串的公共属性模型
末级
公共级摩托车
公共财产示例作为示例()
公共属性描述为字符串
末级
公车
公共财产示例作为示例()
公共属性描述为字符串
末级
公营货车
公共财产示例作为示例()
公共属性描述为字符串
末级
公营车辆
公共财产摩托车作为摩托车
公共财产汽车
公共财产货车
末级
公营车辆
作为车辆的公共财产车辆
末级
Dim内容作为字符串
'反序列化JSON
使用streamReader作为streamReader=newstreamreader(“C:\Users\idiot\Desktop\example.json”)
'以字符串形式获取文件的全部内容。
contents=streamReader.ReadToEnd()
终端使用
Dim CarObject As VehiclerRoot=新的JavaScriptSerializer()。反序列化(车辆根)(内容)
'可以修改现有的JSON,但如何添加?
CarObject.vehicles.car.example(0).model=“Octavia”
将新数据标注为新列表(示例)
使用新数据
.Add(带有{.make=“Bugatti”,.model=“Veyron”}的新示例)
以
使用{.example=newData.ToArray}将p作为新车进行调整
“我现在卡住了。
“CarObject.vehicles.concat
'重新序列化修改后的JSON
Dim serializer=新的JavaScriptSerializer()
Dim serializedResult=序列化程序。序列化(p)
使用writer作为StreamWriter=
新的StreamWriter(“C:\Users\idiot\Desktop\example\u v2.json”)
writer.Write(序列化结果)
终端使用
我正在使用.NET JavaScriptSerializer。我已经查看了JSON.net中的代码示例,以防那里有我可以/应该使用的东西,但实际上看不到任何东西


非常感谢您提供的任何帮助或为我指明正确方向(VB或C#)。

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

数组。调整大小

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

ReDim保存CarObject.vehicles.car.example(CarObject.vehicles.car.example.Length)
然后将一辆新车添加到空阵列插槽:

CarObject.vehicles.car.example(CarObject.vehicles.car.example.Length-1)=带有{
.make=“布加迪”,
.model=“威龙”
}

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

Dim newData作为新列表(示例)
使用新数据
.Add(带有{.make=“Bugatti”,.model=“Veyron”}的新示例)
.Add(带有{.make=“Ford”,.model=“Focus”}的新示例)
以
数组。调整大小

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)
然后循环:

i=0到newData.Count-1的

尺寸j为整数=CarObject.vehicles.car.example.Length-newData.Count+i
CarObject.vehicles.car.example(j)=newData(i)
下一个

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

数组。调整大小

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

ReDim保存CarObject.vehicles.car.example(CarObject.vehicles.car.example.Length)
然后将一辆新车添加到空阵列插槽:

CarObject.vehicles.car.example(CarObject.vehicles.car.example.Length-1)=带有{
.make=“布加迪”,
.model=“威龙”
}

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

Dim newData作为新列表(示例)
使用新数据
.Add(带有{.make=“Bugatti”,.model=“Veyron”}的新示例)
.Add(带有{.make=“Ford”,.model=“Focus”}的新示例)
以
数组。调整大小

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)
然后循环:

i=0到newData.Count-1的

尺寸j为整数=CarObject.vehicles.car.example.Length-newData.Count+i
CarObject.vehicles.car.example(j)=newData(i)
下一个

工作正常!非常感谢你!上帝啊,朱尔@巴辛巴辛:很高兴我能帮忙!上帝朱尔·奥赫·戈特·尼特奥拉!;)很好用!非常感谢你!上帝啊,朱尔@巴辛巴辛:很高兴我能帮忙!上帝朱尔·奥赫·戈特·尼特奥拉!;)