[英]How to convert a Class object to JSON Format using C#
I Want to convert inherited Class object into Json Format using C# Code 我想使用C#代码将继承的Class对象转换为Json格式
Here is the inherited class that i am converting into json 这是我正在转换为json的继承的类
public class RetrieveSanctionPartyListResponse : BaseResponse
{
public RetrieveSanctionPartyList RetrieveSanctionPartyList { get; set; }
}
In the above class it has another class here is the code of that class 在上面的类中,它还有另一个类,这是该类的代码
public class RetrieveSanctionPartyList
{
public string Spl { get; set; }
public string Tech { get; set; }
public string PartnerId { get; set; }
}
Inherited class 继承的类
public class BaseResponse
{
public bool Success { get; set; }
public List<string> Messages { get; set; }
public BaseResponse()
{
Messages = new List<string>();
}
public virtual string ToSerialize()
{
string txXML;
using (var ms = new MemoryStream())
{
var ser = new DataContractSerializer(GetType());
ser.WriteObject(ms, this);
ms.Position = 0;
var sr = new StreamReader(ms);
txXML = sr.ReadToEnd();
//txXML = "\"" + txXML.Replace("\"", "\\\"") + "\"";
sr.Close();
}
return txXML;
}
}
Using Json.NET, you can serialize an instance of your class RetrieveSanctionPartyListResponse
: 使用Json.NET,您可以序列化类
RetrieveSanctionPartyListResponse
的实例:
var partyListResponse = new RetrieveSanctionPartyListResponse();
var serializedObject = JsonConvert.SerializeObject(partyListResponse);
=== UPDATE === === 更新 ===
(.Net 4.0 or higher) Reference your project to System.Runtime.Serialization.dll (.Net 4.0或更高版本)将您的项目引用到System.Runtime.Serialization.dll
Change your classes to: 将您的班级更改为:
RetrieveSanctionPartyList.cs RetrieveSanctionPartyList.cs
[DataContract]
public class RetrieveSanctionPartyList
{
[DataMember]
public string Spl { get; set; }
[DataMember]
public string Tech { get; set; }
[DataMember]
public string PartnerId { get; set; }
}
BaseResponse.cs BaseResponse.cs
[DataContract]
public class BaseResponse
{
[DataMember]
public bool Success { get; set; }
[DataMember]
public List<string> Messages { get; set; }
public BaseResponse()
{
Messages = new List<string>();
}
public static T fromJson<T>(string json)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer js = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(json));
T obj = (T)js.ReadObject(ms);
return obj;
}
public string toJson()
{
System.Runtime.Serialization.Json.DataContractJsonSerializer js = new System.Runtime.Serialization.Json.DataContractJsonSerializer(this.GetType());
MemoryStream ms = new MemoryStream();
js.WriteObject(ms, this);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
return sr.ReadToEnd();
}
public virtual void print()
{
Console.WriteLine("Class: " + this.GetType());
Console.WriteLine("[Success] " + this.Success);
foreach (string str in this.Messages)
Console.WriteLine("[Messages] " + str);
}
}
RetrieveSanctionPartyListResponse .cs RetrieveSanctionPartyListResponse .cs
public class RetrieveSanctionPartyListResponse : BaseResponse
{
[DataMember]
public RetrieveSanctionPartyList RetrieveSanctionPartyList { get; set; }
public override void print()
{
base.print();
Console.WriteLine("[RetrieveSanctionPartyList.Spl] " + RetrieveSanctionPartyList.Spl);
Console.WriteLine("[RetrieveSanctionPartyList.Tech] " + RetrieveSanctionPartyList.Tech);
Console.WriteLine("[RetrieveSanctionPartyList.PartnerId] " + RetrieveSanctionPartyList.PartnerId);
}
}
Test program: 测试程序:
static void Main()
{
BaseResponse obj = new BaseResponse();
obj.Messages.Add("4");
obj.Messages.Add("2");
obj.Messages.Add("3");
obj.print();
string json = obj.toJson();
Console.WriteLine("Json: " + json);
BaseResponse clone = BaseResponse.fromJson<BaseResponse>(json);
Console.WriteLine("Clone: ");
clone.print();
RetrieveSanctionPartyListResponse child1 = new RetrieveSanctionPartyListResponse();
child1.Success = true;
child1.Messages.Add("Only one");
RetrieveSanctionPartyList list = new RetrieveSanctionPartyList();
list.Spl = "MySPL";
list.PartnerId = "MyPartnerId";
list.Tech = "MyTech";
child1.RetrieveSanctionPartyList = list;
child1.print();
json = child1.toJson();
Console.WriteLine("Json: " + json);
RetrieveSanctionPartyListResponse cloneChild1 = BaseResponse.fromJson<RetrieveSanctionPartyListResponse>(json);
Console.WriteLine("cloneChild1: ");
cloneChild1.print();
Console.ReadLine();
}
Ouput: 输出继电器:
Class: WindowsForms.BaseResponse
类:WindowsForms.BaseResponse
[Success] False
[成功]错误
[Messages] 4
[留言] 4
[Messages] 2
[留言] 2
[Messages] 3
[留言] 3
Json: {"Messages":["4","2","3"],"Success":false}
Json:{“消息”:[“ 4”,“ 2”,“ 3”],“成功”:false}
Clone:
克隆:
Class: WindowsForms.BaseResponse
类:WindowsForms.BaseResponse
[Success] False
[成功]错误
[Messages] 4
[留言] 4
[Messages] 2
[留言] 2
[Messages] 3
[留言] 3
Class: WindowsForms.RetrieveSanctionPartyListResponse
类:WindowsForms.RetrieveSanctionPartyListResponse
[Success] True
[成功]正确
[Messages] Only one
[留言]只有一个
[RetrieveSanctionPartyList.Spl] MySPL
[RetrieveSanctionPartyList.Spl] MySPL
[RetrieveSanctionPartyList.Tech] MyTech
[RetrieveSanctionPartyList.Tech] MyTech
[RetrieveSanctionPartyList.PartnerId] MyPartnerId
[RetrieveSanctionPartyList.PartnerId] MyPartnerId
Json: {"Messages":["Only one"],"Success":true,"RetrieveSanctionPartyList":{"Part
杰森:{“消息”:[“仅一个”],“成功”:真,“ RetrieveSanctionPartyList”:{“部分
nerId":"MyPartnerId","Spl":"MySPL","Tech":"MyTech"}}
nerId “:” MyPartnerId”, “杀破狼”: “MySPL”, “技术”: “明泰”}}
cloneChild1:
cloneChild1:
Class: WindowsForms.RetrieveSanctionPartyListResponse
类:WindowsForms.RetrieveSanctionPartyListResponse
[Success] True
[成功]正确
[Messages] Only one
[留言]只有一个
[RetrieveSanctionPartyList.Spl] MySPL
[RetrieveSanctionPartyList.Spl] MySPL
[RetrieveSanctionPartyList.Tech] MyTech
[RetrieveSanctionPartyList.Tech] MyTech
[RetrieveSanctionPartyList.PartnerId] MyPartnerId
[RetrieveSanctionPartyList.PartnerId] MyPartnerId
Yet another way to convert ac# object to JSON - via JavaScriptSerializer 将AC#对象转换为JSON的另一种方法-通过JavaScriptSerializer
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(new {Field1="Value1"});
For a list of differences between DataContractJsonSerializer and JavaScriptSerializer see here. 有关DataContractJsonSerializer和JavaScriptSerializer之间的差异列表,请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.