Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将对象的复杂属性序列化为标量值_C#_Json_Json.net - Fatal编程技术网

C# 将对象的复杂属性序列化为标量值

C# 将对象的复杂属性序列化为标量值,c#,json,json.net,C#,Json,Json.net,假设我们有以下类定义 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

假设我们有以下类定义

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
}

您的代码将无法编译,因为您正在为int值分配一个B类 但我想我得到了你想要的:

 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序列化程序使用所需的名称序列化数据,因为您正在将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 serializer使用所需的名称序列化数据

此问题的正确解决方案是使用可处理您的类型的自定义
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);
        }
    }
}
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
var a=新的a();
var str=JsonConvert.SerializeObject(一个新的JsonSerializerSettings()
{
转换器=新列表()
{
新的BTypeJsonConverter()
}
});
}
}
公共A类
{
公共字符串名称{get;}=“Johny Bravo”;
公共B ComplexProperty{get;}=new B();
}
公共B级
{
公共字符串Prop1{get;}=“value1”;
公共字符串Prop2{get;}=“value2”;
公共int Prop3{get;}=100;
公共重写字符串ToString()
{
返回此.Prop3.ToString();
}
}
公共类B类型JsonConverter:JsonConverter
{
公共重写void WriteJson(JsonWriter编写器、对象值、JsonSerializer序列化器)
{
var b=作为b的值;
如果(b==null)返回;
WriteValue(b.ToString());
}
public override object ReadJson(JsonReader reader,Type objectType,object existingValue,
JsonSerializer(序列化程序)
{
抛出新的NotImplementedException();
}
公共覆盖布尔CanConvert(类型objectType)
{
返回objectType==typeof(B);
}
}
}

解决此问题的正确方法是使用可处理您的类型的自定义
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);
        }
    }
}
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
var a=新的a();
var str=JsonConvert.SerializeObject(一个新的JsonSerializerSettings()
{
转换器=新列表()
{
新的BTypeJsonConverter()
}
});
}
}
公共A类
{
公共字符串名称{get;}=“Johny Bravo”;
公共B ComplexProperty{get;}=new B();
}
公共B级
{
公共字符串Prop1{get;}=“value1”;
公共字符串Prop2{get;}=“value2”;
公共int Prop3{get;}=100;
公共重写字符串ToString()
{
返回此.Prop3.ToString();
}
}
公共类B类型JsonConverter:JsonConverter
{
公共重写void WriteJson(JsonWriter编写器、对象值、JsonSerializer序列化器)
{
var b=作为b的值;
如果(b==null)返回;
WriteValue(b.ToString());
}
public override object ReadJson(JsonReader reader,Type objectType,object existingValue,
JsonSerializer(序列化程序)
{
抛出新的NotImplementedException();
}
公共覆盖布尔CanConvert(类型objectType)
{
返回objectType==typeof(B);
}
}
}

添加另一个属性,并将序列化属性名称设置为第一个属性。如果您的类
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();
    }
}

添加另一个属性并将序列化
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();
    }
}

谢谢你的回答。我确实想过这样的事情,但如果A和B来自第三方图书馆呢?@MihailShishkov,这没什么区别。你有get方法,所以你可以得到你想要的任何值。像public int complexValue=>SomeMethod(someObjects);在第三方库中有A和B意味着您不能更改它们的代码。因此,您不能只向它们添加新属性或属性。如果我只是为了序列化而在A周围提供一个适配器,您的建议就会起作用,这对我来说似乎是一个很大的工作。想象一下,如果A是一个具有很多属性的真实类:)我找到了一种方法。检查我的答案。谢谢你的回答。我确实想过这样的事情,但如果A和B来自第三方图书馆呢?@MihailShishkov,这没什么区别。你有get方法,所以你可以得到你想要的任何值。像public int complexValue=>SomeMethod(someObjects);在第三方库中有A和B意味着您不能更改它们的名称