如何在C#中将多个对象作为单个json传递?

编程入门 行业动态 更新时间:2024-10-17 09:43:15
本文介绍了如何在C#中将多个对象作为单个json传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将多个对象作为单个json格式传递我的对象如下所示 public class Emp { public string Id {得到;组; } public string Name {get;组; } } 现在我需要传递json,如下所示 [ {Id:John,姓名:美国}, {Id:John,姓名:US}, {Id:John,姓名:US}, {Id:John, 姓名:美国} ]

我有什么尝试过: 我尝试使用下面的代码,但获取单个json对象的正确数据。 但我需要在方括号内获取多个数据。 您能否建议如何使用逗号分隔来获取多个对象。

使用Newtonsoft.Json; 使用System; 使用System.Collections.Generic; namespace ToCommaExample { class program { static void Main(string [] args) { Service objService = new Service( ); 服务objService2 = new Service(); var listData = new List< SecurityAuditData> { new SecurityAuditData {TransactionName =UserAdded, EntityID =useradded@test, UserIdentity =identity1}, }; var listData2 = new List< SecurityAuditData> { new SecurityAuditData {TransactionName =SecurityRoleUpdated, EntityID =SecurityRoleUpdated@test, UserIdentity =identity2}, }; objService.AuditData = listData; objService2.AuditData = listData2; var finalJson = JsonConvert.SerializeObject(objService2.AuditData,Formatting.Indented); Console.WriteLine(finalJson); Console.Read(); } } 公共类SecurityAuditData { public string TransactionName {get;组; } 公共字符串EntityID {get;组; } 公共字符串UserIdentity {get;组; } } 类服务 { [NonSerialized] 公共对象AuditData; } }

现在输出如下

[ {TransactionName:SecurityRoleUpdated,EntityID:SecurityRoleUpdated@test,UserIdentity:identity2} ]

预期输出

[ {TransactionName:SecurityRoleUpdated ,EntityID:SecurityRoleUpdated@test,UserIdentity:identity2}, {TransactionName:UserAdded ,EntityID:useradded@test,UserIdentity:identity1} ]

解决方案

是的非常简单你可以把这个列表传递给JSON序列化 使用Newtonsoft.Json; var json = JsonConvert.SerializeObject(yourlist_here); 为此你必须通过NuGet将它安装到你的项目中包管理器(工具 - > NuGet包管理器 - >包管理器控制台): PM>安装包Newtonsoft.Json

做类似的事情 var finalJson = json.Serialize(classObj);

How to pass multiple object as single json format my object like below public class Emp { public string Id{ get; set; } public string Name{ get; set; } } Now i need to pass json like below [ { "Id": "John", "Name": "US" }, { "Id": "John", "Name": "US" }, { "Id": "John", "Name": "US" }, { "Id": "John", "Name": "US" } ]

What I have tried: I have tried with the below code but getting proper data for single json object. But i need to get multiple data within square brackets. Could you please suggest how to proceed to get multiple object with comma separated.

using Newtonsoft.Json; using System; using System.Collections.Generic; namespace ToCommaExample { class Program { static void Main(string[] args) { Service objService = new Service(); Service objService2 = new Service(); var listData = new List<SecurityAuditData> { new SecurityAuditData {TransactionName = "UserAdded", EntityID = "useradded@test", UserIdentity = "identity1"}, }; var listData2 = new List<SecurityAuditData> { new SecurityAuditData {TransactionName = "SecurityRoleUpdated", EntityID = "SecurityRoleUpdated@test", UserIdentity = "identity2"}, }; objService.AuditData = listData; objService2.AuditData = listData2; var finalJson = JsonConvert.SerializeObject(objService2.AuditData, Formatting.Indented); Console.WriteLine(finalJson); Console.Read(); } } public class SecurityAuditData { public string TransactionName { get; set; } public string EntityID { get; set; } public string UserIdentity { get; set; } } class Service { [NonSerialized] public object AuditData; } }

Present output like below

[ { "TransactionName": "SecurityRoleUpdated", "EntityID": "SecurityRoleUpdated@test", "UserIdentity": "identity2" } ]

Expected output

[ { "TransactionName": "SecurityRoleUpdated", "EntityID": "SecurityRoleUpdated@test", "UserIdentity": "identity2" }, { "TransactionName": "UserAdded", "EntityID": "useradded@test", "UserIdentity": "identity1" } ]

解决方案

yes its very simple you can pass this list to JSON Serialize using Newtonsoft.Json; var json = JsonConvert.SerializeObject(yourlist_here); for this you have to install this into your project via the NuGet Package Manager (Tools --> NuGet Package Manager --> Package Manager Console): PM> Install-Package Newtonsoft.Json

Do something like this var finalJson = json.Serialize(classObj);

更多推荐

如何在C#中将多个对象作为单个json传递?

本文发布于:2023-07-14 19:57:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   中将   对象   如何在   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!