序列化您的数据
添加另一个属性并将序列化的
假设我们有以下类定义
public class A
{
public string Name { get; } = "Johny Bravo";
public B ComplexProperty { get; } = new B();
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}
序列化时
var a = new A();
var str = JsonConvert.SerializeObject(a);
它将产生以下json字符串
{
"Name":"Johny Bravo",
"ComplexProperty":{
"Prop1":"value1",
"Prop2":"value2",
"Prop3":100
}
}
我们如何使ComplexProperty序列化为标量值?想要的结果必须是这样的:
{
"Name":"Johny Bravo",
"ComplexProperty":100
}
您的代码将不会编译,因为您正在将B类分配给int值但我想我得到了你想要的:
class Program
{
static void Main(string[] args)
{
var a = new A();
var str = JsonConvert.SerializeObject(a);
Console.Write(str);
}
}
public class A
{
public string Name { get; } = "Johny Bravo";
[JsonIgnore]
public B ComplexProperty { get; } = new B();
[JsonProperty("ComplexProperty")]
public int complexValue => ComplexProperty.Prop3;
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}
由于您希望对象是平面的(只有1级深度),因此需要在A类上具有该属性。由于您不想在json结果中包含您的复杂对象,因此必须忽略它,并且由于您需要在json结果上使用相同的名称,因此必须告诉json序列化程序使用所需的名称
这个问题的正确解决方案是使用可以处理您的类型的自定义JsonConverter
。尤其是在您无法控制A和B类的代码的情况下。这是的样本代码
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var a = new A();
var str = JsonConvert.SerializeObject(a, new JsonSerializerSettings()
{
Converters = new List<JsonConverter>()
{
new BTypeJsonConverter()
}
});
}
}
public class A
{
public string Name { get; } = "Johny Bravo";
public B ComplexProperty { get; } = new B();
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}
public class BTypeJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var b = value as B;
if (b == null) return;
writer.WriteValue(b.ToString());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(B);
}
}
}
PropertyName
设置为第一个属性。如果类A
来自第三方库,请创建一个从A
派生的类并添加新属性。
public class A
{
public string Name { get; } = "Johny Bravo";
--> [JsonIgnore]
public B ComplexProperty { get; } = new B();
-->[JsonProperty(PropertyName = "ComplexProperty")]
--> public string ComplexPropertyText { get{ return ComplexProperty.ToString(); } }
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}