Reputation: 102
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.
Upvotes: 2
Views: 307
Reputation: 129807
Json.Net DOES support deserializing key-value pairings: you just need to use a Dictionary<string, T>
, where T
can be object
, JToken
or some other class that can receive the data values (string
works in your case). With a dictionary it is actually quite easy to deserialize and iterate over the values, as shown below:
class Program
{
static void Main(string[] args)
{
string json = @"
{
""identification"": {
""lineItem/latestDate"": ""2013-04-28"",
""lineItem/destination"": ""test"",
""lineItem/quantity"": ""55"",
}
}";
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
foreach (KeyValuePair<string, string> kvp in obj.identification)
{
Console.WriteLine("type: " + kvp.Key);
Console.WriteLine("value: " + kvp.Value);
Console.WriteLine();
}
}
}
class RootObject
{
public Dictionary<string, string> identification { get; set; }
}
Output:
type: lineItem/latestDate
value: 2013-04-28
type: lineItem/destination
value: test
type: lineItem/quantity
value: 55
Upvotes: 2
Reputation: 1130
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.
Upvotes: 4
Reputation: 1130
Here, I found one more way of doing it and the good news is we do not have to change key names.
create a class at client side:
public class RootObject
{
public Dictionary<string,string> identification { get; set; }
}
Now, Deserialize it:
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);
Bingo, We have done it again.
1) Now, ob object has desired values.
2) Works with array of values.
3) Supports iteration too.
Upvotes: 5