我遇到了以下情况,有人可以帮我实现以下目标吗?
I met a situation as below could anybody help me achieve as below?
例如,如果我有课程:-
For Example, if I have the class:-
public class Sample
{
public String name {get;set;}
public MyClass myclass {get;set;}
}
我的 Myclass
如下:
public class MyClass
{
public String p1 {get;set;}
public String p2 {get;set;}
}
当我使用 Json.net
对 Sample 类的对象进行序列化时,得到如下结果,效果很好.
When I am using Json.net
to Serialize the object of the class Sample,I got as below and it works well.
{
"name":"...",
"myclass":
{
"p1":"...",
"p2":"..."
}
}
它是正确的,但我想知道是否有可能得到如下的 json 字符串?
Its correct but I wonder is it possible to get the json string as below?
{
"name":"...",
"p1":"...",
"p2":"..."
}
你可以创建匿名对象并序列化它:
You can create anonymous object and serialize it:
var sample = new Sample {
name = "Bob",
myclass = new MyClass {
p1 = "x",
p2 = "y"
}};
string json = JsonConvert.SerializeObject(new {
sample.name,
sample.myclass.p1,
sample.myclass.p2
});
结果
{"name":"Bob","p1":"x","p2":"y"}
但我建议您使用 Sample
类的默认序列化,或创建将序列化为您的格式的类(即将 MyClass
属性移动到 Sample
).
But I suggest you either use default serialization of your Sample
class, or create class which will be serialized into your format (i.e. move MyClass
properties into Sample
).
更新:您可以使用自定义转换器,它将对象展平并将所有内部对象属性序列化为顶级对象属性:
UPDATE: You can use custom converter, which flattens object and serializes all inner objects properties as top level object properties:
public class FlattenJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
return;
}
JObject o = (JObject)t;
writer.WriteStartObject();
WriteJson(writer, o);
writer.WriteEndObject();
}
private void WriteJson(JsonWriter writer, JObject value)
{
foreach (var p in value.Properties())
{
if (p.Value is JObject)
WriteJson(writer, (JObject)p.Value);
else
p.WriteTo(writer);
}
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return true; // works for any type
}
}
用法:
string json = JsonConvert.SerializeObject(sample, new FlattenJsonConverter());
或者你可以简单地将匿名类型创建隐藏到自定义转换器中,如果你只需要一种类型的这种行为:
Or you can simply hide anonymous type creation into custom converter, if you need this behavior for one type only:
public class SampleJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
{
Sample sample = (Sample)value;
JToken t = JToken.FromObject(new {
sample.name,
sample.myclass.p1,
sample.myclass.p2
});
t.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Sample);
}
}
这篇关于使用 json.net 在序列化期间合并两个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!