問題描述
我遇到了以下情況,有人可以幫我實現以下目標嗎?
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模板網!