I am struggling to change one list in to the form of Key and Value.
As an example: I have a List Property object
public List<Details> Details{get;set;}
public class Details
{
public string FirstName{get;set;}
public string LastName{get;set;}
}
I am looking for a JSON string using this object in the below Format:
{"Details":[
{"Key":"FirstName" ,"Value":"value in list object"},
{"Key":"LastName" ,"Value":"value in list object"}
]}
I am unable to achieve it using JSON serialization. Is there any way to achieve it using any type of available serialization ?
Thanks guys in advance.
To be begin with, the output sample given isn't quite a valid Json as its missing "{" and "}" at either end.
You could achieve the above format using Custom Json Converter and a wrapping Anonymous type before serializing. For example,
Consider the following JsonConverter.
public class KeysJsonConverter : JsonConverter
{
private readonly Type[] _types;
public KeysJsonConverter(params Type[] types)
{
_types = types;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken token = JToken.FromObject(value);
if(token.Type == JTokenType.Object)
{
JObject oldValue = (JObject)token;
JObject newValue = new JObject();
var jkey = oldValue.Properties().Select(x=>x.Name).First();
var jvalue = oldValue.Properties().Select(x=>x.Value).First();
newValue.AddFirst(new JProperty("Key",jkey));
newValue.Add(new JProperty("Value",jvalue));
newValue.WriteTo(writer);
}
return;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanRead
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return _types.Any(t => t == objectType);
}
}
You can now serialize the collection using following.
var result = JsonConvert.SerializeObject(details,
Newtonsoft.Json.Formatting.Indented,
new KeysJsonConverter(typeof(Details)));
This would produce an output as following.
[
{
"Key": "FirstName",
"Value": "Anu"
},
{
"Key": "FirstName",
"Value": "Jia"
}
]
In order to wrap the Json with "Details" key as in OP, you could wrap your collection within an anonymous type. For example,
var result = JsonConvert.SerializeObject(new {Details=details},
Newtonsoft.Json.Formatting.Indented,
new KeysJsonConverter(typeof(Details)));
Output Sample,
{
"Details": [
{
"Key": "FirstName",
"Value": "Anu"
},
{
"Key": "FirstName",
"Value": "Jia"
}
]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.