json将新对象添加到现有json文件C# [英] json add new object to existing json file C#

查看:459
本文介绍了json将新对象添加到现有json文件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动将新对象添加到现有json文件中.我在网上四处张望,却发现添加了数据和东西,但没有发现整个对象.这就是我要编辑的文件的外观:

I'm trying to automate the adding of new objects to an existing json file. I looked all around the web but only found adding data and stuff but not an whole object. This is how the file that i wanna edit looks:

[
    {"id":"123","name":"carl"}
]

我想去

[
     {"id":"123","name":"carl"},
     {"id":"1234","name":"carl2"}
]

谢谢您的所有回答,但我认为所有人都没有完全理解我的意思.我已经尝试了一些答案,但是后来我明白了:

Thank you for all your answers but i don't think everyone is completely understanding what i mean i have tried some of the answers but then i get this:

[
    "{\"id\":\"123\",\"name\":\"carl\"}"
]"{\"id\":\"1234\",\"name\":\"carl2\"}"

我想在[]之间保持一切.

and i want everthing inbetween the [].

推荐答案

如果您使用 json.NET ,则可以简单地反序列化并序列化json.

If you use json.NET you can simply deserialize and serialize the json.

var list = JsonConvert.DeserializeObject<List<Person>>(myJsonString);
list.Add(new Person(1234,"carl2");
var convertedJson = JsonConvert.SerializeObject(list, Formatting.Indented);

这篇关于json将新对象添加到现有json文件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆