title: Serialize List<KeyValuePair<string, string>> as JSON
body: <p>I'm very new with JSON, please help!</p>
  
  <p>I am trying to serialise a <code>List&lt;KeyValuePair&lt;string, string&gt;&gt;</code> as JSON</p>
  
  <p>Currently:</p>
  
  <pre><code>[{"Key":"MyKey 1","Value":"MyValue 1"},{"Key":"MyKey 2","Value":"MyValue 2"}]
  </code></pre>
  
  <p>Expected:</p>
  
  <pre><code>[{"MyKey 1":"MyValue 1"},{"MyKey 2":"MyValue 2"}]
  </code></pre>
  
  <p>I referred to some examples from <a href="https://stackoverflow.com/questions/21021655/json-serialize-listkeyvaluepairstring-object">this</a> and <a href="https://stackoverflow.com/questions/8030538/how-to-implement-custom-jsonconverter-in-json-net-to-deserialize-a-list-of-base">this</a>.</p>
  
  <p>This is my KeyValuePairJsonConverter : JsonConverter</p>
  
  <pre><code>public class KeyValuePairJsonConverter : JsonConverter
  {
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          List&lt;KeyValuePair&lt;object, object&gt;&gt; list = value as List&lt;KeyValuePair&lt;object, object&gt;&gt;;
          writer.WriteStartArray();
          foreach (var item in list)
          {
              writer.WriteStartObject();
              writer.WritePropertyName(item.Key.ToString());
              writer.WriteValue(item.Value.ToString());
              writer.WriteEndObject();
          }
          writer.WriteEndArray();
      }
  
      public override bool CanConvert(Type objectType)
      {
          return objectType == typeof(List&lt;KeyValuePair&lt;object, object&gt;&gt;);
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          var jsonObject = JObject.Load(reader);
          var target = Create(objectType, jsonObject);
          serializer.Populate(jsonObject.CreateReader(), target);
          return target;
      }
  
      private object Create(Type objectType, JObject jsonObject)
      {
          if (FieldExists("Key", jsonObject))
          {
              return jsonObject["Key"].ToString();
          }
  
          if (FieldExists("Value", jsonObject))
          {
              return jsonObject["Value"].ToString();
          }
          return null;
      }
  
      private bool FieldExists(string fieldName, JObject jsonObject)
      {
          return jsonObject[fieldName] != null;
      }
  }
  </code></pre>
  
  <p>I am calling it from a WebService method like this</p>
  
  <pre><code>List&lt;KeyValuePair&lt;string, string&gt;&gt; valuesList = new List&lt;KeyValuePair&lt;string, string&gt;&gt;();
  Dictionary&lt;string, string&gt; valuesDict = SomeDictionaryMethod();
  
  foreach(KeyValuePair&lt;string, string&gt; keyValue in valuesDict)
  {
      valuesList.Add(keyValue);
  }
  
  JsonSerializerSettings jsonSettings = new JsonSerializerSettings { Converters = new [] {new KeyValuePairJsonConverter()} };
  string valuesJson = JsonConvert.SerializeObject(valuesList, jsonSettings);
  </code></pre>