如何在C#中将多个对象作为单个json传递? [英] How to pass multiple object as single json in C#?
本文介绍了如何在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.com,
UserIdentity =identity1},
};
var listData2 = new List< SecurityAuditData>
{
new SecurityAuditData {TransactionName =SecurityRoleUpdated,
EntityID =SecurityRoleUpdated@test.com,
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.com,
UserIdentity:identity2
}
]
预期输出
[
{
TransactionName:SecurityRoleUpdated ,
EntityID:SecurityRoleUpdated@test.com,
UserIdentity:identity2
},
{
TransactionName:UserAdded ,
EntityID:useradded@test.com,
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.com", UserIdentity = "identity1"}, }; var listData2 = new List<SecurityAuditData> { new SecurityAuditData {TransactionName = "SecurityRoleUpdated", EntityID = "SecurityRoleUpdated@test.com", 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.com", "UserIdentity": "identity2" } ]
Expected output
[ { "TransactionName": "SecurityRoleUpdated", "EntityID": "SecurityRoleUpdated@test.com", "UserIdentity": "identity2" }, { "TransactionName": "UserAdded", "EntityID": "useradded@test.com", "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传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文