您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用JSON填充现有对象

使用JSON填充现有对象

是的,您可以使用JsonConvert.PopulateObject()第二个JSON字符串来填充现有对象的属性

这是一个例子:

string json1 = @"
{
    ""CID"": ""13579"",
    ""jsonrpc"": ""something"",
    ""id"": ""24680""
}";

Account account = JsonConvert.DeserializeObject<Account>(json1);

string json2 = @"
{
    ""mail"": [ ""abc@example.com"", ""def@example.org"" ],
    ""uid"": [ ""87654"", ""192834"" ],
    ""userPassword"": [ ""superSecret"", ""letMeInNow!"" ]
}";

JsonConvert.PopulateObject(json2, account);

Console.WriteLine("CID: " + account.CID);
Console.WriteLine("jsonrpc: " + account.jsonrpc);
Console.WriteLine("id: " + account.id);
Console.WriteLine("mail: " + string.Join(", ", account.mail));
Console.WriteLine("uid: " + string.Join(", ", account.uid));
Console.WriteLine("userPassword: " + string.Join(", ", account.userPassword));

输出

CID: 13579
jsonrpc: something
id: 24680
mail: abc@example.com, def@example.org
uid: 87654, 192834
userPassword: superSecret, letMeInNow!

小提琴:https ://dotnetfiddle.net/621bfV

其他 2022/1/1 18:14:29 有483人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶