写json“ value”:“ value”

时间:2019-03-11 09:00:53

标签: c# .net json serialization

我在.net核心项目中使用了newtonsoft,并且遇到了一种特殊的json格式。 我有一个列表,序列化后出现的“普通” json是这样的:

  {
      "id": "0124",
      "entities": [
        {
          "name": "chris",
          "surname": "green"
        },
        {
          "name": "albert",
          "surname": "red"
        }
      ]
    }

我需要将其转换为:

  {
      "id": "0124",
      "entities": [
        {
          "chris": "green",
          "albert": "red"
        }
      ]
    }

我不知道列表中可以有多少个实体。

谢谢大家

编辑

感谢帮助人员,所以代码如下:

var jsonBody = JsonConvert.SerializeObject(
                new BulkRegisterDto
                {
                    Id = "0124",
                    Entities = entities
                }
            );


public class BulkRegisterDto
    {
        [JsonProperty(PropertyName = "id")]
        public string Id{ get; set; }
        [JsonProperty(PropertyName = "entities")]
        public IList<Person> Entities { get; set; }
    }

 public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }

这些不是真实属性。我正在尝试使用动态JObject。我们会看看它是否有效

2 个答案:

答案 0 :(得分:2)

@Aleks Andreev我将您的答案设置为正确的答案,因为它可以解决问题,但我更喜欢一种更好的方法,并使用@MindSwipe注释创建了自定义json转换器。

这是我的下面的代码:

public class BulkEntityConverter : JsonConverter
    {
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            var obj = (JObject)JObject.ReadFrom(reader);

            JProperty property = obj.Properties().FirstOrDefault();
            // I didn't use this method
            return new BulkRegisterDto
            {
                Id = property.Name,
                Entities = new List<Person>()
            };
        }

        public override void WriteJson(
            JsonWriter writer,
            object value,
            JsonSerializer serializer)
        {
            BulkRegisterDto permission = (BulkRegisterDto)value;

            var innerEntities = new JObject();
            foreach (var entry in permission.Entities)
            {
                innerEntities.Add(entry.Name, entry.Surname);
            }

            var root = new JObject
            {
                { "id", permission.Id},
                { "entities", new JArray { innerEntities } }
            };

            root.WriteTo(writer);
        }

        public override bool CanConvert(Type t)
        {
            return typeof(BulkRegisterDto).IsAssignableFrom(t);
        }

        public override bool CanRead
        {
            get { return true; }
        }
    }

使用这种方式,我可以获得一个清晰的类,可以在哪里使用我的json转换器,如:

var jsonBody = JsonConvert.SerializeObject(manager, new BulkEntityConverter());

其中manager是我的BulkRegisterDto对象

感谢所有支持人员

答案 1 :(得分:1)

您可以将json反序列化为DTO:

[DataContract]
private class Person
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "surname")]
    public string Surname { get; set; }
}

[DataContract]
private class Entities
{
    [DataMember(Name = "entities")]
    public Person[] Persons { get; set; }
}

,然后使用原始JObjectJArray

构建预期的json
var input = "{\r\n  \"entities\": [\r\n    {\r\n      \"name\": \"chris\",\r\n      \"surname\": \"green\"\r\n    },\r\n    {\r\n      \"name\": \"albert\",\r\n      \"surname\": \"red\"\r\n    }\r\n  ]\r\n}";
var json = JsonConvert.DeserializeObject<Entities>(input);

var root = new JObject();
var innerObject = new JObject();

root.Add("entities", new JArray {innerObject});

foreach (var entry in json.Persons)
    innerObject.Add(entry.Name, entry.Surname);

如果您致电root.ToString(),您会得到

{
  "entities": [
    {
      "chris": "green",
      "albert": "red"
    }
  ]
}