使用C#和Json.NET根据条件序列化JSON对象 [英] Serialize JSON object as per the condition using C# and Json.NET

查看:106
本文介绍了使用C#和Json.NET根据条件序列化JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON.Net将数据库中的数据序列化为JSON格式.但是我无法获得预期的结果.

I am serializing data from a database into JSON format using JSON.Net. But I am not able to get the result I expect.

我必须根据某些条件创建JSON对象,例如如果datamap的数据为null,则不应将其包含在JSON中;如果其为非null,则应将其包含在内.

I have to create JSON objects according to certain conditions like if my data for datamap is null then it should not be included in the JSON, and if it is not null then it should be included.

public class DatamapKey
{
    [JsonExtensionData]
    public Dictionary<string, JToken> DatamapKeyFields = new Dictionary<string, JToken>();
}
public class DatamapKey1
{
    [JsonExtensionData]
    public Dictionary<string, JToken> DatamapKey1Fields = new Dictionary<string, JToken>();
}
public class DatamapItem
{
    [JsonExtensionData]
    public Dictionary<string, JToken> DatamapItemFields = new Dictionary<string, JToken>();
    public DatamapKey datamapKey { get; set; }
    public DatamapKey1 datamapKey1 { get; set; }
}
public class RootObject
{
    public List<DatamapItem> datamapItems { get; set; }
}

输出JSON:

{
  "datamapItems": [
    {
      "datamapKey": {
        "module": 1,
        "id": 1391
      },
      "datamapKey1": {},
      "paramName": "VE8321C",
      "min": "0",
      "max": "40"
    },
    {
      "datamapKey": {},
      "datamapKey1": {},
      "paramName": "VE8321C",
      "min": "0",
      "max": "40"
    },
    {
      "datamapKey": {
        "module": 1,
        "id": 1391
      },
      "datamapKey1": {
        "module": 1,
        "id": 1391
      },
      "paramName": "VE8321C",
      "min": "0",
      "max": "40"
    }
  ]
}

预期输出:

{
  "datamapItems": [
    {
      "paramName": "VE8321C",
      "datamapKey": {
        "module": 1,
        "id": 1391
      },
      "min": "0",
      "max": "40"
    },
    {
      "paramName": "VE8321C",
      "min": "0",
      "max": "40"
    },
    {
      "paramName": "VE8321C",
      "datamapKey": {
        "module": 1,
        "id": 1391
      },
      "datamapKey1": {
        "module": 1,
        "id": 1391
      },
      "min": "0",
      "max": "40"
    }
  ]
}

推荐答案

[JsonExtensionData]属性使被标记字典中的键/值对被序列化,就好像它们是包含字典的类的属性一样.此属性由Json.Net专门处理,因此@Alex在评论中建议的 IgnoreEmptyEnumerablesResolver 对它的影响.但是您可以重组类,这样就不需要datamapKeydatamapKey1的属性,这将使解析器在这些字典为空时可以对其进行处理,从而为您提供所需的输出.

The [JsonExtensionData] attribute causes the key-value pairs in the marked dictionary to be serialized as if they were properties of the class containing the dictionary. This attribute is handled specially by Json.Net and thus the IgnoreEmptyEnumerablesResolver suggested by @Alex in the comments will not have an effect on it. But you could restructure your classes such that you don't need the attribute for datamapKey and datamapKey1, and that would allow the resolver to work on those dictionaries when they are empty, giving you the output you want.

public class DatamapItem
{
    [JsonExtensionData]
    public Dictionary<string, JToken> DatamapItemFields { get; set; } = new Dictionary<string, JToken>();
    public Dictionary<string, JToken> datamapKey { get; set; } = new Dictionary<string, JToken>();
    public Dictionary<string, JToken> datamapKey1 { get; set; } = new Dictionary<string, JToken>();
}

public class RootObject
{
    public List<DatamapItem> datamapItems { get; set; }
}

演示小提琴: https://dotnetfiddle.net/Gw03gY

如果您不喜欢该主意或不想修改您的类结构,另一个选择是将您的对象实例加载到JObject中,并使用递归方法从层次结构中删除空标记. 如何在生成的JSON中忽略/忽略/跳过空对象文字?.

If you don't like that idea or don't want to modify your class structure, another option is to load your object instance into a JObject and use a recursive method to remove empty tokens from the hierarchy. This approach is covered in detail in How to omit/ignore/skip empty object literals in the produced JSON?.

这里有一个演示小提琴,用您现有的类结构显示了该方法: https://dotnetfiddle.net/jmNgYi

Here is a demo fiddle showing that approach with your existing class structure: https://dotnetfiddle.net/jmNgYi

这篇关于使用C#和Json.NET根据条件序列化JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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