We have a JSON structure where we send an array of key value pairings from our server to the client. For brevity and tidiness, we send the data like this:
"identification": {
"lineItem/latestDate": "2013-04-28",
"lineItem/destination": "test",
"lineItem/quantity": "55"
}
instead of this:
"identification": [
{
"value": "test",
"type": "lineItem/destination"
},
{
"value": "2014-07-25",
"type": "lineItem/latestDate"
},
{
"value": "55",
"type": "lineItem/quantity"
}
]
The problem is our downstream client (who are processing the JSON using C#) say they cannot handle the first format, as the built in json.net functionality does not support it, and they do not know of another that can do it.
Is it possible to deserialize the first format using the built in JSON deserialization in C#? Or does anyone know of another library that can take care of this? I had a look at this question and to me it looks possible.
1) Given first JSON is not valid, requires left brace at starting and one extra comma to be removed from last.
2) If your client can use Newtonsoft.Json and you can use '_' instead of '/' in keys, because identifiers with '/' within name not supported in c#
Client Side:
create classes as
public class Identification
{
public string lineItem_latestDate { get; set; }
public string lineItem_destination { get; set; }
public string lineItem_quantity { get; set; }
}
public class RootObject
{
public Identification identification { get; set; }
}
NOTE: if you have more key/value pairs you can add more properties to class Identification
Now, Deserialize it as:
string data = @"[
{
""identification"": {
""lineItem_latestDate"": ""2013-04-28"",
""lineItem_destination"": ""test"",
""lineItem_quantity"": ""55""
}
},
{
""identification"": {
""lineItem_latestDate"": ""2013-04-28"",
""lineItem_destination"": ""test"",
""lineItem_quantity"": ""55""
}
},
{
""identification"": {
""lineItem_latestDate"": ""2013-04-28"",
""lineItem_destination"": ""test"",
""lineItem_quantity"": ""55""
}
}]";
List<RootObject> ob = JsonConvert.DeserializeObject<List<RootObject>>(data);
Done, now ob object has values according to data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With