使用 json.net 在序列化期间合并两个对象?

Merge two objects during serialization using json.net?(使用 json.net 在序列化期间合并两个对象?)
本文介绍了使用 json.net 在序列化期间合并两个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了以下情况,有人可以帮我实现以下目标吗?

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 在序列化期间合并两个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)