Decode nested JSON objects in C# - c#

I am trying to convert a nested JSON object on an ASP.NET server. The incoming JSON string looks something like this -
data: {
user_id: 1,
taskid: "1234",
list: {
"item-1": { one: 1, two: 2 },
"item-2": { one: 1, two: 2 }
//.. where number of items is unknown
}
}
I have tried to decode the data using JSON.Decode this way
public class Data {
public int user_id { get; set; }
public string taskid { get; set; }
public List<object> list { get; set; }
}
public class DataList {
List<Data> data { get; set; }
}
// if isPost etc..
var decodedData = JSON.Decode<DataList>(Request["data"])
But when I try and iterate over decodedData I am getting an error -
foreach statement cannot operate on variables of type
'ASP._Page_that_cshtml.DataList' because
'ASP._Page_that_cshtml.DataList' does not contain a public definition
for 'GetEnumerator'
When I try casting the decodedData to a List this way -
List<Data> decodedData = JSON.Decode<DataList>(Request["data"])
I throw another error
CS0030: Cannot convert type 'ASP._Page_that_cshtml.DataList' to 'System.Collections.Generic.List<ASP._Page_that_cshtml.DataList>'
Could you please suggest an appropriate method to convert nested JSON objects into a C# object and iterating over it?
PS: trailing semi-colons omitted on-purpose

List<Data> decodedData = JSON.Decode<DataList>(Request["data"])
Should Be
var decodedData = JSON.Decode<List<Data>>(Request["data"])
var myDataList = new DataList() { data = decodedData; }

Your example is not valid json. You should have a collection [] for list:
data: {
"user_id": 1,
"taskid": "1234",
"list": [
{
"one": 1,
"two": 2
},
{
"one": 1,
"two": 2
}
]
}

The first error you are getting is quite right, your class DataList does not contain a definition for GetEnumerator which is required for a foreach statement. You will need to iterate over the property, so iterate over decodedData.data.
The second error is again correct, as you are trying to cast DataList to a type of List, something C# has no idea how to do. You would again need to create your DataList, then set the property data to the type List.

Try iterating over decodedData.data instead.
decodedData is a DataList, and the DataList class has a member data which is a List<Data>. List<Data> has a GetEnumerator method, as required by foreach.
Alternatively, you could add a GetEnumerator method to DataList:
public IEnumerator GetEnumerator() { return data.GetEnumerator(); }

You can try decoding the JSON into an array of Data objects, and then calling the ToList() extension method on that array.
var dataArray = JSON.Decode<Data[]>(Request["data"]);
var list = dataArray.ToList();

Related

Error deserializing a json when an array is empty

I am trying to deserialize a json but when the array ("groups") is empty it gives me an error. How can I check in Unity that if there is no data in the "groups" field a message will appear, for example?
This is the error:
InvalidCastException: Specified cast is not valid.
BC_Equipos.successCallbackGMGI (System.String jsonResponse, System.Object cbObject) (at Assets/_AG/Scripts/BC_Equipos.cs:249)
The error line 249 is: var groups = (Dictionary<string, object>[])jsonData["groups"];
When there is data in "groups" all works fine.
The json is:
{
"data": {
"requested": [],
"invited": [],
"groups": []
},
"status": 200
}
Here is how I'm deserializating:
var jsonMessage = (Dictionary<string, object>)BrainCloud.JsonFx.Json.JsonReader.Deserialize(jsonResponse);
var jsonData = (Dictionary<string, object>)jsonMessage["data"];
var groups = (Dictionary<string, object>[])jsonData["groups"];
if (groups.Length > 0)
{
Debug.Log("Groups List");
foreach (var group in groups)
{
var groupId = group["groupId"];
Debug.Log("groupId: " + groupId);
var nameGroup = group["name"];
Debug.Log("nameGroup: " + nameGroup);
}
}
else
{
Debug.Log("User hasn't joined a group already!");
}
If the array is empty, the tool can't infer what kind of array to create - typical data in the general case could, after all, be ["abc", "def"], [12, 42, 42], or - as seems to be the case - [{ }, { }]. It could even be something hybrid like ["abc", 42, {}]. You may have better luck just casting to Array - or perhaps object[], checking the length, and going from there. If object[] doesn't work: try Array, IList, or similar.
Or: a much better approach is to create a type model, and deserialize into that; then the serializer can use the defined model to interpret the data. For example:
public class SomeRoot {
public SomeData data {get;set;}
public int status {get;set;}
}
public class SomeData {
public List<Request> requested {get;} = new();
public List<Invite> invited {get;} = new();
public List<Group> groups {get;} = new();
}
public class Request {}
public class Invite {}
public class Group {}

Deserialize JSON string in to multiple C# objects

I have a JSON string in below format for which I want to deserialize it into C# List. But the record number "1","2","3" (it can be upto 1,2,3...n depends on the json response each time) in JSON restricting me to deserialize it into C# object using Newtonsoft.Json
{
"1":{
"UID":"1",
"LICENCENO":"licenseno",
"NAME":"ABC"
},
"2":{
"UID":"2",
"LICENCENO":"licenseno",
"NAME":"PQR"
},
"3":{
"UID":"3",
"LICENCENO":"licenseno",
"NAME":"XYZ"
}
}
I am using below code for deserialization
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DriverMaster>>(json);
I have DriverMaster class created as-
public class DriverMaster
{
public string UID { get; set; }
public string LICENCENO { get; set; }
public string NAME { get; set; }
}
Deserialization line giving unhandled exception, I know I am doing it in wrong way, because DriverMaster json object cannot be extracted into c# directly without doing something to record number 1,2,3...n in c#. Can anyone please help me to sort it out? Thanks in advance.
You were close:
var result = JsonConvert.DeserializeObject<Dictionary<string, DriverMaster>>(json)
.Select(x => x.Value)
.ToList();
Solution.
Change your code to use...
var result = JsonConvert.DeserializeObject<Dictionary<int, DriverMaster>>(json);
Explaination
The type is not the same... The List<DriverMaster>type will convert to JSON like so...
{
"1":
{
"DriverMaster": {
"UID":"1",
"LICENCENO":"licenseno",
"NAME":"ABC"
}
}
}
This doesn't match what you showed in your question...
The type that you are looking for is actually Dictionary<int, DriverMaster>, which is a key/value pair which will output a JSON string like so
{
"1": { ... },
"2": { ... },
"3": { ... }
}
In order to fix that, you need to use the Dictionary<int, DriverMaster> type instead.
For these types of things I like to use the often overlooked feature of JToken.SelectTokens. This function allows you to select tokens within a json string and permits the use of wildcards.
Here's some code that will deserialize your sample by selecting past the 1,2,3...N in the json:
public static IEnumerable<DriverMaster> Deserialize(string json)
{
return JToken.Parse(json).SelectTokens("*")
.Select(jToken => jToken.ToObject<DriverMaster>());
}
The * basically says to select all tokens after the root, so it's selecting the values associated with 1, 2, 3.. etc... Here's another SO answer that shows a more complicated usage of the SelectTokens method.
You need to use
public class DriverMaster
{
public string UID { get; set; }
public string LICENCENO { get; set; }
public string NAME { get; set; }
}
public class Root
{
[JsonExtensionData]
public IDictionary<string,JToken> Data {get;set;}
}
and
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(json);
If you want to have result as List, you can parse the result as.
var list = new List<DriverMaster>();
foreach(KeyValuePair<string, JToken> token in result.Data)
{
list.Add(token.Value.ToObject<DriverMaster>());
}
That would give you the desired result as
1 licenseno ABC
2 licenseno PQR
3 licenseno XYZ

C# JsonConvert.DeserializeObject to list / add item to array

I'm currently trying to add a item to a array. But I think a list would be way easier since I could use
list.Add("whatever");
Is there a way to receive the following as a list?
dynamic reps = JsonConvert.DeserializeObject("rep.json");
Example json:
{"reps": {
{
"username": "usera",
"reps": 10,
"latestrep": "userx"
},
"userb": {
"username": "userb",
"reps": 10,
"latestrep": "userx"
},
"userc": {
"username": "userc",
"reps": 10,
"latestrep": "userx"
}
}}
I appreciate any kind of help
Providing you have a class defining the list item:
public class UserReps
{
public string Username { get; set; }
public int Reps { get; set; }
public string LatestRep { get; set; }
}
You can achieve what you want with LINQ:
IDictionary<string, IDictionary<string, UserReps>> parsed = JsonConvert.DeserializeObject<IDictionary<string, IDictionary<string, UserReps>>>(json);
List<UserReps> userReps = parsed["reps"].Select(ur => ur.Value).ToList();
I think you are looking for something like this:
`
public static dynamic FromJsonToDynamic(this object value)
{
dynamic result = value.ToString().FromJsonToDynamic();
return result;
}
`
That is an extension method that I use for generic objects. If you are looking at something to convert it to a particular object you could create an extension method like this:
public static T FromJson<T>(this object value) where T : class
{
T result = value.ToString().FromJson<T>();
return result;
}
With your data as it is at the moment, no. Your input data doesnt appear to be a collection or something that could logically be 'listified'.

Error when deserializing JSON to Object

I need to convert JSON data that I get from a REST API and convert them to CSV for some analytic. The problem is that the JSON data do not necessarily follow the same content, so I can't define a type for mapping. This has become a challenge that is taking too much of my time. I have already created some code, but of course it is not working as it throws exception on this line
var data = JsonConvert.DeserializeObject<List<object>>(jsonData);
The error is:
Additional information: Cannot deserialize the current JSON object
(e.g. {"name":"value"}) into type
'System.Collections.Generic.List`1[System.Object]' because the type
requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 2, position 10.
please let me know what I can do to get this going.
A sample of data would be like this, the fields of data can change very often, for example a new field can be added the next day, so I don't have the liberty to create a .Net class to map the data.
{
"data": [
{
"ID": "5367ab140026875f70677ab277501bfa",
"name": "Happiness Initiatives - Flow of Communication/Process & Efficiency",
"objCode": "PROJ",
"percentComplete": 100.0,
"plannedCompletionDate": "2014-08-22T17:00:00:000-0400",
"plannedStartDate": "2014-05-05T09:00:00:000-0400",
"priority": 1,
"projectedCompletionDate": "2014-12-05T08:10:21:555-0500",
"status": "CPL"
},
{
"ID": "555f452900c8b845238716dd033cf71b",
"name": "UX Personalization Think Tank and Product Strategy",
"objCode": "PROJ",
"percentComplete": 0.0,
"plannedCompletionDate": "2015-12-01T09:00:00:000-0500",
"plannedStartDate": "2015-05-22T09:00:00:000-0400",
"priority": 1,
"projectedCompletionDate": "2016-01-04T09:00:00:000-0500",
"status": "APR"
},
{
"ID": "528b92020051ab208aef09a4740b1fe9",
"name": "SCL Health System - full Sitecore implementation (Task groups with SOW totals in Planned hours - do not bill time here)",
"objCode": "PROJ",
"percentComplete": 100.0,
"plannedCompletionDate": "2016-04-08T17:00:00:000-0400",
"plannedStartDate": "2013-11-04T09:00:00:000-0500",
"priority": 1,
"projectedCompletionDate": "2013-12-12T22:30:00:000-0500",
"status": "CPL"
}
]
}
namespace BusinessLogic
{
public class JsonToCsv
{
public string ToCsv(string jsonData, string datasetName)
{
var data = JsonConvert.DeserializeObject<List<object>>(jsonData);
DataTable table = ToDataTable(data);
StringBuilder result = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(table.Columns[i].ColumnName);
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(row[i].ToString());
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
}
return result.ToString().TrimEnd(new char[] {'\r', '\n'});
}
private DataTable ToDataTable<T>( IList<T> data )
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0 ; i < values.Length ; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}
}
The real issue here is that you are trying to deserialize into a List<object> but your JSON actually represents a single object containing a data property which then contains a list of objects. That is why you are getting this error. Json.Net can't deserialize a single object into a list. I think what you really want to do is define a container class like this:
class Root
{
public List<Dictionary<string, object>> Data { get; set;}
}
Then deserialize like this:
var data = JsonConvert.DeserializeObject<Root>(jsonData).Data;
You will then end up with a list of dictionaries, where each dictionary represents one item in the JSON array. The dictionary key-value pairs are the dynamic values in each item. You can then work with these as you would with any other dictionary. For example, here is how you would dump out all the data:
foreach (var dict in data)
{
foreach (var kvp in dict)
{
Console.WriteLine(kvp.Key + ": " + kvp.Value);
}
Console.WriteLine();
}
Fiddle: https://dotnetfiddle.net/6UaKhJ
What you're looking for is the dynamic type. Though unrelated, this answer contains much of the information on how you'll be able to iterate through the changing properties on your object.
You will need to add some additional work to figure out how to handle your result when it is an array versus a single object as your error shows us. However, this is a good first step for you.
Basically, a dynamic object is a Dictionary, much like how a JSON object is treated in JavaScript. You just need to iterate through each of the KeyValuePair objects within the main object and go through their properties.
var data = JsonConvert.DeserializeObject<dynamic>(jsonData);
var rows = new List<string>();
// Go through the overall object, and get each item in
// the array, or property in a single object.
foreach (KeyValuePair<string, object> item in data)
{
dynamic obj = item.Value;
var row = "";
// Perhaps add a check here to see if there are more
// properties (if it is an item in an array). If not
// then you are working with a single object, and each
// item is a property itself.
foreach (KeyValuePair<string, object> prop in obj)
{
// Very dummy way to demo adding to a CSV
string += prop.Value.ToString() + ",";
}
rows.Add(string);
}
This is far from a complete example, but we don't have enough information to go on to help you finish what you're trying to do.
You are trying to deserialize into a List but your JSON actually represents a single object containing a data property containing list of objects. That is why you are getting this error. Json.Net can't deserialize a single object into a list.
Please try this:Create a class which contain single property on datatype Object and pass this class for deserialization.
class Parent
{
public object Data { get; set;}
}
Then deserialize like this:
var output = JsonConvert.DeserializeObject<Parent>(jsonData);
Try using this class instead of Object
public class Datum
{
public string ID { get; set; }
public string name { get; set; }
public string objCode { get; set; }
public double percentComplete { get; set; }
public string plannedCompletionDate { get; set; }
public string plannedStartDate { get; set; }
public int priority { get; set; }
public string projectedCompletionDate { get; set; }
public string status { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
}
Change to this:
var data = JsonConvert.DeserializeObject<RootObject>(jsonData);
If your data is dynamic so try a dynamic list:
using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
var d=jss.Deserialize<dynamic>(str);
Since you're trying to deserialize an object type into a list type, it won't deseralize directly.
You can do this:
var data = JsonConvert.DeserializeObject<ObjectDataList>(jsonData);
var rows = new List<DeserializedData>();
foreach (dynamic item in data)
{
var newData = new DeserializedData();
foreach (dynamic prop in item)
{
var row = new KeyValuePair<string, string>
(prop.Name.ToString(), prop.Value.ToString());
newData.Add(row);
}
rows.Add(newData);
}
Here are new classes
//class for key value type data
class DeserializedData
{
List<KeyValuePair<string, string>> NewData =
new List<KeyValuePair<string, string>>();
internal void Add(KeyValuePair<string, string> row)
{
NewData.Add(row);
}
}
[DataContract]
class ObjectDataList
{
[DataMember(Name ="data")]
List<object> Data { get; set; }
public IEnumerator<object> GetEnumerator()
{
foreach (var d in Data)
{
yield return d;
}
}
}
As far as I can tell, more recent versions of Newtonsoft can actually do this now, no additional work required.
I was working with the binary version however, and this did still have the issue - I had a test where you could configure to use binary or json, and the json version worked just fine, but the binary complained about not getting an array type.
I started using the BsonDataReader for this, and was looking through the properties and methods on it to see how I could best look at the contents, and lo and behold, this had a property called:
reader.ReadRootValueAsArray
Setting this to 'true' did the trick.

How to get object json when deserializing array

I have an incoming JSON, which consists array of some objects, say, Foo. I deserialize them with
result = JsonConvert.DeserializeObject<List<Foo>>(message);
Now i want to add a string property to Foo, which will store it's JSON (which i received), so that Foo'll look like:
public class Foo
{
public int MyInt { get; set; }
public bool MyBool { get; set; }
public string JSON { get; set; }
}
But i don't know how can i say JSON.Net the way it can populate such a field..
UPD
I'll clearify what i want. Say i receive JSON:
[{"MyInt":1,"MyBool":0},{"MyInt":2,"MyBool":0},{"MyInt":3,"MyBool":1}]
Here is array of 3 objects and i want, when deserializing, to add corresponding part of json to object, so that:
First object will contain {"MyInt":1,"MyBool":0}
Second object will contain {"MyInt":2,"MyBool":0}
Third object will contain {"MyInt":3,"MyBool":1}
in their JSON Property
I'll be gratefull for any help!
This is one way to do it, but it doesn't maintain the exact original JSON - but it does provide a static record of the original JSON (but without the exact format of the original values - i.e. Bool maybe be 0/1 or true/false):
var message = #"[{""MyInt"":1,""MyBool"":0},{""MyInt"":2,""MyBool"":0},{""MyInt"":3,""MyBool"":1}]";
var foos = JsonConvert.DeserializeObject<List<Foo>>(message);
var t = JsonConvert.SerializeObject(foos[0]);
foos = foos.Select(s => new Foo() { MyBool = s.MyBool, MyInt = s.MyInt, JSON = JsonConvert.SerializeObject(s) }).ToList();
If you are dealing with a lot of Foos, then you might want to find a more efficient way. There might be a way to 'update' using linq, rather than creating a new list.
Okay, i found an answer. I didn't know that i can deserialize message into JArray and then enumerate it (good job, newtonsoft:) ). Here is what i endede up with:
if (tokenType is JArray)
{
var arr = JsonConvert.DeserializeObject(message) as JArray;
foreach (var item in arr)
{
try
{
var agentParameter = item.ToObject<Foo>();
agentParameter.JSON = item.ToString();
result.Add(agentParameter);
}
catch (Exception)
{
LogProvider.Error(string.Format("Failed to Deserialize message. Message text: \r\n {0}", item.ToString()));
}
}
}

Categories