How to handle differing JSON using Newtonsoft [duplicate] - c#

This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Create a strongly typed c# object from json object with ID as the name
(1 answer)
How to handle both a single item and an array for the same property using JSON.net
(9 answers)
Closed 3 years ago.
I am consuming an API response and the response contains a list of items. Here is example JSON
{
"attendeeid": "1",
"responses": {
"1": {
"questionid": "1",
"fieldname": "1",
"name": "question?",
"pageid": "2",
"page": "Attendee Information",
"auto_capitalize": "0",
"choicekey": "",
"response": [
" Identify need",
" Evaluate products and services"
]
},
"2": {
"questionid": "2",
"fieldname": "2",
"name": "question2",
"pageid": "2",
"page": "Attendee Information",
"auto_capitalize": "0",
"choicekey": "live",
"response": "live"
},
},
}
As you can see, the response field in the response can be an object, or a string.
How can I create a POCO object which can be deserialized into? Currently my class is
public class RegistrantInfoResponse
{
public string questionid { get; set; }
public string fieldname { get; set; }
public string name { get; set; }
public string response { get; set; }
}
public class RegistrantInfo
{
public string attendeeid { get; set; }
public List<RegistrantInfoResponse> responses { get; set; }
}

There are couple of modifications you could in your class definition. The RegistrantInfo.responses should be defined as
public Dictionary<string, RegistrantInfoResponse> responses { get; set; }
This would enable you to use dynamic keys as in the responses (Example, "1","2").
If your observe RegistrantInfoResponse.response, it is either an string or an collection of strings. You could use an Custom Converter as provided in the example here by Brian, which converts a string to a List. This would require to decorate the property with an attribute as the following.
[JsonConverter(typeof(SingleOrArrayConverter<string>))]
public List<string> response { get; set; }
The complete class definition would be as the following.
public class RegistrantInfoResponse
{
public string questionid { get; set; }
public string fieldname { get; set; }
public string name { get; set; }
[JsonConverter(typeof(SingleOrArrayConverter<string>))]
public List<string> response { get; set; }
}
public class RegistrantInfo
{
public string attendeeid { get; set; }
public Dictionary<string, RegistrantInfoResponse> responses { get; set; }
}
Sharing the CustomConverter defined by Brian
class SingleOrArrayConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(List<T>));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Array)
{
return token.ToObject<List<T>>();
}
return new List<T> { token.ToObject<T>() };
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You could now deserialize the json as
var result = JsonConvert.DeserializeObject<RegistrantInfo>(json);
Output Sample

Related

Deserializing a JSON string to a C# class where JSON class may accept either array / object based on a JSON property

I have to deserialize a JSON string to C# classes inorder to bind to a grid. I have implemented the respective classes in C#. But at a particular instance, this fails because the JSON property will be either an array or an object. Please check a part of the string.
I have created ItemList class with 3 properties IL1 , Name and another object class "Item". However, you can see that when the property "Name" is Rubber, I should have List of Item class as a property rather than Item object. When it is Rubber, it returns array of 2 items.
"ItemList": [
{
"#IL1": "Yes",
"#Name": "Pencil"
"Item": {
"#ItemType": "Pencil",
"#Item2": "1A7716744F7048ACA2549BE93F0A2BF1",
"aimLink": {
"#id": "1A7716744F7048ACA2549BE93F0A2BF1",
"#DisplayText": "P00001"
}
}
},
{
"#IL1": "Yes",
"#Name": "Pen",
"Item": {
"#ItemType": "Pen",
"#Item2": "AE067F7EDB6147C09AED243C1F7FAD25",
"aimLink": {
"#id": "AE067F7EDB6147C09AED243C1F7FAD25",
"#DisplayText": "5100010654120001
}
}
},
{
"#IL1": "Yes",
"#Name": "Rubber",
"Item": [
{
"#ItemType": "Rubber",
"#ItemGID": "622025629037499394DF092DA16BAB7F",
"aimLink": {
"#id": "622025629037499394DF092DA16BAB7F",
"#DisplayText": "12345678-1234-123456-7116#01"
}
},
{
"#ItemType": "Rubber",
"#ItemGID": "F336F65F8E014E80B84A2312F829493C"
"aimLink": {
"#id": "F336F65F8E014E80B84A2312F829493C",
"#DisplayText": "12345678-1234-123456-7116#14"
}
}
]
}
],
How can I parse this to a C# class effectively and the easiest way to get this done?
Thanks,
Divya
You can resolve this issue by making your custom JsonConverter and you can use below to convert single value to array then mark Item property with the JsonConverter attribute
public class SingleValueArrayConverter<T> : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
object retVal = new Object();
if (reader.TokenType == JsonToken.StartObject)
{
T instance = (T)serializer.Deserialize(reader, typeof(T));
retVal = new List<T>() { instance };
}
else if (reader.TokenType == JsonToken.StartArray)
{
retVal = serializer.Deserialize(reader, objectType);
}
return retVal;
}
public override bool CanConvert(Type objectType)
{
return false;
}
}
let's assume this your ItemList class
public class ItemList
{
public string #IL1 { get; set; }
public string #Name { get; set; }
[JsonConverter(typeof(SingleValueArrayConverter<Item>))]
public List<Item> Item { get; set; }
}
public class Item
{
public string #ItemType { get; set; }
public string #Item2 { get; set; }
public AimLink aimLink { get; set; }
}
public class AimLink
{
public string #id { get; set; }
public string #DisplayText { get; set; }
}
You can make your custom JsonConverter (e.g. ItemConverter) and mark Item property with the [JsonConverter(typeof(ItemConverter))] attribute

JSON complex type that can be an object or an array of objects [duplicate]

This question already has answers here:
How to handle both a single item and an array for the same property using JSON.net
(9 answers)
Closed 3 years ago.
I am trying to process an object that can be either an array of object or just the object. Using the code below only works when the naics is an object and not an array. What am I doing wrong?
Here is the shortest example I can come up with:
[{
"section": "52.219-1.b",
"naics": [{
"naicsName": "Engineering Services",
"isPrimary": true,
"ExcpCounter": 1,
"isSmallBusiness": "Y",
"naicsCode": 541330
},
{
"naicsName": "Military and Aerospace Equipment and Military Weapons",
"isPrimary": true,
"ExcpCounter": 2,
"isSmallBusiness": "Y",
"naicsCode": 541330
}
]
},
{
"section": "52.219-1.b",
"naics": {
"naicsName": "Janitorial Services",
"isPrimary": true,
"isSmallBusiness": "Y",
"naicsCode": 561720
}
}
]
I will only have one of the types but I forced two in an array to force it into Quick Type.
My classes are:
[JsonProperty("naics", NullValueHandling = NullValueHandling.Ignore)]
public AnswerNaics Naics { get; set; }
public partial struct AnswerNaics
{
public AnswerNaic Naic;
public AnswerNaic[] NaicArray;
public static implicit operator AnswerNaics(AnswerNaic Naic) => new AnswerNaics { Naic = Naic };
public static implicit operator AnswerNaics(AnswerNaic[] NaicArray) => new AnswerNaics { NaicArray = NaicArray };
}
public partial class AnswerNaic
{
[JsonProperty("naicsName")]
public string NaicsName { get; set; }
[JsonProperty("hasSizeChanged")]
public string HasSizeChanged { get; set; }
[JsonProperty("isPrimary")]
public bool IsPrimary { get; set; }
[JsonProperty("ExcpCounter", NullValueHandling = NullValueHandling.Ignore)]
public long? ExcpCounter { get; set; }
[JsonProperty("isSmallBusiness")]
public string IsSmallBusiness { get; set; }
[JsonProperty("naicsCode")]
public string NaicsCode { get; set; }
}
internal class NaicsConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(AnswerNaics) || t == typeof(AnswerNaics?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.StartObject:
var objectValue = serializer.Deserialize<AnswerNaic>(reader);
return new AnswerNaics { Naic = objectValue };
case JsonToken.StartArray:
var arrayValue = serializer.Deserialize<AnswerNaic[]>(reader);
return new AnswerNaics { NaicArray = arrayValue };
}
throw new Exception("Cannot unmarshal type AnswerNaics");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (AnswerNaics)untypedValue;
if (value.NaicArray != null)
{
serializer.Serialize(writer, value.NaicArray);
return;
}
if (value.Naic != null)
{
serializer.Serialize(writer, value.Naic);
return;
}
throw new Exception("Cannot marshal type Naics");
}
public static readonly NaicsConverter Singleton = new NaicsConverter();
}
I have more object or array nodes, but I am just trying to figure out one to be able to apply to all of them.
Since you cannot change the incoming JSON, you're going to need a custom converter instead. Something like this for example:
public class NaicsConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(Naics);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
var naics = new Naics();
switch (reader.TokenType)
{
case JsonToken.StartObject:
// We know this is an object, so serialise a single Naics
naics.Add(serializer.Deserialize<Naic>(reader));
break;
case JsonToken.StartArray:
// We know this is an object, so serialise multiple Naics
foreach(var naic in serializer.Deserialize<List<Naic>>(reader))
{
naics.Add(naic);
}
break;
}
return naics;
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And the supporting classes:
public class Root
{
public string Section { get; set; }
[JsonConverter(typeof(NaicsConverter))]
public Naics Naics { get; set; }
}
// This isn't ideal, but it's quick and dirty and should get you started
public class Naics : List<Naic>
{
}
public class Naic
{
public string NaicsName { get; set; }
public bool IsPrimary { get; set; }
public string IsSmallBusiness { get; set; }
public long NaicsCode { get; set; }
}
And finally, to deserialise:
var settings = new JsonSerializerSettings {Converters = {new NaicsConverter()}};
var root = JsonConvert.DeserializeObject<Root[]>(Json, settings);
Now your object will get serialised into the list, but as a single item.
You can solve this using a dynamic field in your class.
Consider this JSON:
[
{
"field1": "val1",
"nested": [
{
"nestedField": "val2"
},
{
"nestedField": "val3"
}
]
},
{
"field1": "val4",
"nested":
{
"nestedField": "val5"
}
}
]
nested field is first an array with 2 objects and in the second appearance a single object. (similar to the JSON you posted)
So the class representation would look like:
public class RootObject
{
public string field1 { get; set; }
public dynamic nested { get; set; }
public List<NestedObject> NestedObjects
{
get
{
if(nested is JArray)
{
return JsonConvert.DeserializeObject<List<NestedObject>>(nested.ToString());
}
var obj = JsonConvert.DeserializeObject<NestedObject>(nested.ToString());
return new List<NestedObject> { obj };
}
}
}
public class NestedObject
{
public string nestedField { get; set; }
}
The Deserialization code is trivial using Newtonsoft JSON:
var objectList = JsonConvert.DeserializeObject<List<RootObject>>("some_json");
foreach(var v in objectList)
{
foreach(var n in v.NestedObjects)
{
Console.WriteLine(n.nestedField);
}
}
The only change is an implementation of NestedObjects ready only property. It check if the dynamic object is JArray or object. In any case, it returns a List of nested objects.

Deserialize JSON C# Json.net [duplicate]

This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Closed 8 years ago.
I am trying to deserialize a Json response from an API.
The data looks like this
{
"response": {
"6112": {
"ID": 6112,
"Title": "AdditionalPhotos"
},
"5982": {
"ID": 5982,
"Title": "BikeRide"
},
"total_records": "20",
"returned_count": 10,
"returned_records": "1-10"
}
}
C# class:
public class Products
{
public class Product
{
public string Id { get; set; }
public string Title { get; set; }
}
public Product product { get; set; }
}
public class ss
{
public Dictionary<string, Products.Product> Response { get; set; }
public string total_records { get; set; }
}
Serialization code
ss res = Newtonsoft.Json.JsonConvert.DeserializeObject<ss>(jsonData());
I can get it to work without the total_records entry and below by deserializng to a Dictionary <string , Product>. But I cannot figure out how to get it to work. This is the error I get
Error converting value "20" to type 'Products+Product'. Path 'response.total_records'
I know why I get the error, but I'm unsure how I can proceed without going in and substringing from total_records down. I have no control over the API data.
Edit: you guys are fast, I was still getting to putting the classes up
First you json is not valid one, it should look like this
{
"response":{
"6112":{
"ID":"6112",
"Title":"Additional Photos",
},
"5982":{
"ID":"5982",
"Title":"Bike Ride",
},
"total_records": "20",
"returned_count": "10",
"returned_records": "1-10",
}
}
If you mean the response to contain list it should look like this
{
"response":{
"myArray": [
{
"ID":"6112",
"Title":"Additional Photos",
},
{
"ID":"5982",
"Title":"Bike Ride",
}
],
"total_records": "20",
"returned_count": "10",
"returned_records": "1-10",
}
}
So your code look like this
public class MyArray
{
public string ID { get; set; }
public string Title { get; set; }
}
public class Response
{
public List<MyArray> myArray { get; set; }
public string total_records { get; set; }
public string returned_count { get; set; }
public string returned_records { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
If you have control over API response then please refer to Mzf's answer.
If you don't have control over API then it may not be possible to do this particular deserialization on one go. You might have to loop.
Here's my take.
Update
Modified my approach:
Created a class Response which inherits from Dictionary<string, Product>, and added the metadata parts like total_records, records_count to it's public properties. And created a JsonConverter that can deserialize JObject to Response class.
The logic used for deserialization is quite simple:
Extract the metadata parts like total_records, records_count to variables.
Then remove those metadata from the JObject, so that the key values becomes homogeneous.
Now Json.net will be easily able to serialize JObject to Response object, as key values are homogenous.
Assign the metadata extracted previously to the properties of Response object
public void Deserialize()
{
var json = #"{
'response':{
'6112':{
'ID':6112,
'Title':'Additional Photos',
},
'5982':{
'ID':5982,
'Title':'Bike Ride',
},
'total_records': '20',
'returned_count': 10,
'returned_records': '1-10',
}
}";
var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ss>(json, new ResponseConverter());
}
public class Response : Dictionary<string, Product>
{
public int total_records { get; set; }
public int returned_count { get; set; }
public string returned_records { get; set; }
}
public class Product
{
public string Id { get; set; }
public string Title { get; set; }
}
public class ss
{
public Response Response { get; set; }
}
public class ResponseConverter : Newtonsoft.Json.JsonConverter
{
private Response CreateResponse(Newtonsoft.Json.Linq.JObject jObject)
{
//preserve metadata values into variables
int total_records = jObject["total_records"].ToObject<int>();
var returned_records = jObject["returned_records"].ToObject<string>();
var returned_count = jObject["returned_count"].ToObject<int>();
//remove the unwanted keys
jObject.Remove("total_records");
jObject.Remove("returned_records");
jObject.Remove("returned_count");
//once, the metadata keys are removed, json.net will be able to deserialize without problem
var response = jObject.ToObject<Response>();
//Assign back the metadata to response object
response.total_records = total_records;
response.returned_count = returned_count;
response.returned_records = returned_records;
//.. now person can be accessed like response['6112'], and
// metadata can be accessed like response.total_records
return response;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Response);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var jObject = Newtonsoft.Json.Linq.JObject.Load(reader);
Response target = CreateResponse(jObject);
serializer.Populate(jObject.CreateReader(), target);
return target;
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
In my opinion this is how the JSON file should look like:
{
"response": {
"5982": {
"ID": 5982,
"Title": "BikeRide"
},
"6112": {
"ID": 6112,
"Title": "AdditionalPhotos"
},
"total_records": "20",
"returned_count": 10,
"returned_records": "1-10"
}
}
and this is how the class should look like
public class __invalid_type__5982
{
public int ID { get; set; }
public string Title { get; set; }
}
public class __invalid_type__6112
{
public int ID { get; set; }
public string Title { get; set; }
}
public class Response
{
public __invalid_type__5982 __invalid_name__5982 { get; set; }
public __invalid_type__6112 __invalid_name__6112 { get; set; }
public string total_records { get; set; }
public int returned_count { get; set; }
public string returned_records { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}

JSON .NET deserialize string[] instead of string [duplicate]

This question already has answers here:
How to handle both a single item and an array for the same property using JSON.net
(9 answers)
Closed 7 years ago.
I'm using the Yahoo fantasy sports api. I'm getting a result like this:
"player": [
{
...
"eligible_positions": {
"position": "QB"
},
...
},
{
...
"eligible_positions": {
"position": [
"WR",
"W/R/T"
]
},
...
},
How is it that I can deserialize this?
My code looks like this:
var json = new JavaScriptSerializer();
if (response != null)
{
JSONResponse JSONResponseObject = json.Deserialize<JSONResponse>(response);
return JSONResponseObject;
}
And in my JSONResponse.cs file:
public class Player
{
public string player_key { get; set; }
public string player_id { get; set; }
public string display_position { get; set; }
public SelectedPosition selected_position { get; set; }
public Eligible_Positions eligible_positions { get; set; }
public Name name { get; set; }
}
public class Eligible_Positions
{
public string position { get; set; }
}
When I run this, since eligible_positions can return both a string and a string array, I keep getting the error "Type 'System.String' is not supported for deserialization of an array".
I've also tried turning public string position { get; set; } to public string[] position { get; set; } but I still get an error.
How should I handle this?
I'll use Json.Net. The idea is: "declare position as a List<string> and if the value in json is a string. then convert it to a List"
Code to deserialize
var api = JsonConvert.DeserializeObject<SportsAPI>(json);
JsonConverter
public class StringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
if(reader.ValueType==typeof(string))
{
return new List<string>() { (string)reader.Value };
}
return serializer.Deserialize<List<string>>(reader);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Sample Json
{
"player": [
{
"eligible_positions": {
"position": "QB"
}
},
{
"eligible_positions": {
"position": [
"WR",
"W/R/T"
]
}
}
]
}
Classes (Simplified version)
public class EligiblePositions
{
[JsonConverter(typeof(StringConverter))] // <-- See This
public List<string> position { get; set; }
}
public class Player
{
public EligiblePositions eligible_positions { get; set; }
}
public class SportsAPI
{
public List<Player> player { get; set; }
}

How to handle json that returns both a string and a string array? [duplicate]

This question already has answers here:
How to handle both a single item and an array for the same property using JSON.net
(9 answers)
Closed 7 years ago.
I'm using the Yahoo fantasy sports api. I'm getting a result like this:
"player": [
{
...
"eligible_positions": {
"position": "QB"
},
...
},
{
...
"eligible_positions": {
"position": [
"WR",
"W/R/T"
]
},
...
},
How is it that I can deserialize this?
My code looks like this:
var json = new JavaScriptSerializer();
if (response != null)
{
JSONResponse JSONResponseObject = json.Deserialize<JSONResponse>(response);
return JSONResponseObject;
}
And in my JSONResponse.cs file:
public class Player
{
public string player_key { get; set; }
public string player_id { get; set; }
public string display_position { get; set; }
public SelectedPosition selected_position { get; set; }
public Eligible_Positions eligible_positions { get; set; }
public Name name { get; set; }
}
public class Eligible_Positions
{
public string position { get; set; }
}
When I run this, since eligible_positions can return both a string and a string array, I keep getting the error "Type 'System.String' is not supported for deserialization of an array".
I've also tried turning public string position { get; set; } to public string[] position { get; set; } but I still get an error.
How should I handle this?
I'll use Json.Net. The idea is: "declare position as a List<string> and if the value in json is a string. then convert it to a List"
Code to deserialize
var api = JsonConvert.DeserializeObject<SportsAPI>(json);
JsonConverter
public class StringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
if(reader.ValueType==typeof(string))
{
return new List<string>() { (string)reader.Value };
}
return serializer.Deserialize<List<string>>(reader);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Sample Json
{
"player": [
{
"eligible_positions": {
"position": "QB"
}
},
{
"eligible_positions": {
"position": [
"WR",
"W/R/T"
]
}
}
]
}
Classes (Simplified version)
public class EligiblePositions
{
[JsonConverter(typeof(StringConverter))] // <-- See This
public List<string> position { get; set; }
}
public class Player
{
public EligiblePositions eligible_positions { get; set; }
}
public class SportsAPI
{
public List<Player> player { get; set; }
}

Categories

Resources