Serializing a Dictionary<> object with DataContractJsonSerializer
Try serializing this
var parameters = new{ username = "mike", password = "secret", persist = false}
After adding System.Json as a reference, use this helper class to construct the JSON properties:
public static class JsonHelper{ public static KeyValuePair<string, JsonValue> CreateProperty(string name, dynamic value) { return new KeyValuePair<string, JsonValue>(name, new JsonPrimitive(value)); }}
The following LINQ query will dynamically return the JSON properties as a JsonArray using the Helper class.
var result = from item in parameters select new JsonObject(JsonHelper.CreateProperty(item.Key, item.Value));string json = (new JsonArray(result)).ToString();
Result:
[{\"username\":\"mike\"},{\"password\":\"secret\"},{\"persist\":false}]