JSON Serialize List>

后端 未结 1 2007
逝去的感伤
逝去的感伤 2020-12-01 04:44

I used a Dictionary in a Web API project, which is serializing like that in JSON:

{\"keyname\":{objectvalue},\"keyname2:\"....

Since I have

相关标签:
1条回答
  • 2020-12-01 05:22

    If you use the Newtonsoft Json.NET library you can do the following.

    Define a converter to write the list of key/value pairs the way you want:

    class MyConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            List<KeyValuePair<string, object>> list = value as List<KeyValuePair<string, object>>;
            writer.WriteStartArray();
            foreach (var item in list)
            {
                writer.WriteStartObject();
                writer.WritePropertyName(item.Key);
                writer.WriteValue(item.Value);
                writer.WriteEndObject();
            }
            writer.WriteEndArray();
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // TODO...
        }
    
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(List<KeyValuePair<string, object>>);
        }
    }
    

    Then use the converter:

    var keyValuePairs = new List<KeyValuePair<string, object>>
                        {
                            new KeyValuePair<string, object>("one", 1),
                            new KeyValuePair<string, object>("two", 2),
                            new KeyValuePair<string, object>("three", 3)
                        };
    
    JsonSerializerSettings settings = new JsonSerializerSettings { Converters = new [] {new MyConverter()} };
    string json = JsonConvert.SerializeObject(keyValuePairs, settings);
    

    This generates [{"one":1},{"two":2},{"three":3}]

    0 讨论(0)
提交回复
热议问题