如何使用C#在JSON.net中将JSON对象的深度向上移动一级? [英] How to move JSON object's depth one level up in JSON.net using C#?

查看:130
本文介绍了如何使用C#在JSON.net中将JSON对象的深度向上移动一级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSON.net编写类的转换器

I am trying to write a converter for a class using JSON.net

序列化JsonObject类型的对象时,得到以下输出

When I serialize the object of type JsonObject, I get the following output

{"DataObject":{"id":"1","name":"data name"}}

如何将DataObject向上移动一级以获得以下输出:

How can I move the DataObject one level up to get the following output:

{"id":"1","name":"data name"}

您可以在下面找到相关代码.

You can find the relevant code below.

我的课有以下格式:

public class JsonObject
{
    public JsonObject(IDataObject dataObject)
    {
        this.DataObject= dataObject;
    }

    [JsonConverter(typeof(JsonDataObjectConverter))]
    public IDataObject DataObject;
}

DataObject具有一些属性:

public class MyDataObject : IDataObject
{
    [JsonProperty(PropertyName = "id", Required = Required.Always)]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name", Required = Required.Always)]
    public string Name { get; set; }
}

我从文档中引用了此页面,并写了以下内容转换器:

I referred this page from the documentation and wrote the following converter:

public class JsonDataObjectConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(IDataObject).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        if (value is MyDataObject dataObject)
        {
            writer.WriteStartObject();

            writer.WritePropertyName("id");
            writer.WriteValue(dataObject.Id);
            writer.WritePropertyName("name");
            writer.WriteValue(dataObject.Name);

            writer.WriteEndObject();
        }
    }
}

感谢您的帮助.谢谢.

推荐答案

通过将转换器移至类而不是属性,并使用[JsonIgnore]忽略属性,我能够获得所需的输出.该属性需要忽略,因为该类的转换器将为该属性生成JSON,如下所示.

I was able to get the desired output by moving the converter to the class instead of the property and ignoring the property using [JsonIgnore]. The property needs to be ignored since the converter for the class will generate the JSON for the property as shown below.

所以JsonObject类将是:

[JsonConverter(typeof(JsonObjectConverter))]
public class JsonObject
{
    public JsonObject(IDataObject dataObject)
    {
        this.DataObject= dataObject;
    }

    [JsonIgnore]
    public IDataObject DataObject;
}

然后按以下方式创建转换器:

Then create the converter like this:

public class JsonObjectConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(IDataObject).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JsonObject jsonObject = (JsonObject)value;
        if (jsonObject.DataObject.GetType() == typeof(MyDataObject))
        {
            MyDataObject dataObject = (MyDataObject) jsonObject.DataObject;

            writer.WriteStartObject();

            writer.WritePropertyName("id");
            writer.WriteValue(dataObject.Id);
            writer.WritePropertyName("name");
            writer.WriteValue(dataObject.Name);

            writer.WriteEndObject();
        }
    }
}

这将提供所需的输出:

{"id":"1","name":"data name"}

这篇关于如何使用C#在JSON.net中将JSON对象的深度向上移动一级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆