87,907
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Newtonsoft.Json; //注意引用 Newtonsoft.Josn.dll
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
CommonCode a = new CodeA() { A_CODE = "A1_code", A_NAME = "A1_name" };
CommonCode b = new CodeB() { B_CODE = "A1_code", B_NAME = "B1_name" };
string[] arr = {
Newtonsoft.Json.JsonConvert.SerializeObject(a),
Newtonsoft.Json.JsonConvert.SerializeObject(b)
};
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine("End");
Console.ReadLine();
}
}
public class CodeB : CommonCode
{
[JsonIgnore] //在输出json时忽略
public string B_CODE
{
get
{
return this.Code;
}
set
{
this.Code = value;
}
}
[JsonIgnore]
public string B_NAME
{
get
{
return this.Name;
}
set
{
this.Name = value;
}
}
}
public class CodeA : CommonCode
{
[JsonIgnore]
public string A_CODE
{
get
{
return this.Code;
}
set
{
this.Code = value;
}
}
[JsonIgnore]
public string A_NAME
{
get
{
return this.Name;
}
set
{
this.Name = value;
}
}
}
public class CommonCode
{
public string Code { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
CommonCode a = new CodeA() { A_CODE = "A1_code", A_NAME = "A1_name" };
CommonCode b = new CodeB() { B_CODE = "A1_code", B_NAME = "B1_name" };
string[] arr = {
Newtonsoft.Json.JsonConvert.SerializeObject(a),
Newtonsoft.Json.JsonConvert.SerializeObject(b)
};
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine("End");
Console.ReadLine();
}
}
public class CodeB : CommonCode
{
public string B_CODE
{
get
{
return this.Code;
}
set
{
this.Code = value;
}
}
public string B_NAME
{
get
{
return this.Name;
}
set
{
this.Name = value;
}
}
}
public class CodeA : CommonCode
{
public string A_CODE
{
get
{
return this.Code;
}
set
{
this.Code = value;
}
}
public string A_NAME
{
get
{
return this.Name;
}
set
{
this.Name = value;
}
}
}
public class CommonCode
{
public string Code { get; set; }
public string Name { get; set; }
}
}
for(var o in json1){
json1[o]["CODE"] = json1[o]["A_CODE"];
json1[o]["NAME"] = json1[o]["A_NAME"];
delete json1[o]["A_CODE"];
delete json1[o]["A_NAME"];
}
for(var o in json2){
json2[o]["CODE"] = json2[o]["B_CODE"];
json2[o]["NAME"] = json2[o]["B_NAME"];
delete json2[o]["B_CODE"];
delete json2[o]["B_NAME"];
}