static void Main(string[] args)
{
var json = #"{ ""rows"": [
[
{
""colspan"": 4,
""id"": ""ContentPanel1""
},
{
""colspan"": 8,
""id"": ""ContentPanel2""
}
],
[
{
""colspan"": 12,
""id"": ""ContentPanel3""
}
]
]}";
var json_serializer = new JavaScriptSerializer();
var jsonData = json_serializer.Deserialize<Grid>(json);
Console.ReadKey();
}
[Serializable]
public class Grid
{
public List<Row> rows { get; set; }
}
[Serializable]
public class Row
{
public int colspan { get; set; }
public int id { get; set; }
public List<Row> rows { get; set; }
}
I am trying to convert this JSON string to a C# object, but I am finding it hard because the error message is not very intuitive. Any JSON punters please help!
ERROR Type 'ConsoleApplication1.Program+Row' is not supported for deserialization of an array.
First we get:
Type 'Row' is not supported for deserialization of an array.
The JSON with [ [ shows a nested array. So either change the JSON, or make rows a Row[][]:
public Row[][] rows { get; set; }
Now we get:
ContentPanel1 is not a valid value for Int32.
well...
public int id { get; set; }
vs
""id"": ""ContentPanel1""
Now: "ContentPanel1" is not an int. Make id a string:
public string id { get; set; }
Related
I have little to no experience in JSON and I am stuck with a problem. Any help is appreciated.
I want to access specifically the names' values from the additionalInformation array.
JSON Response:
{
"statusCode": 200,
"version": 1,
"jsonData": [
{
"additionalInformation": [
{
"id": "XXX94XXXX9xxXx_xxxXXXX",
"name": "xxxx xxx x xxxxxxxx"
},
{
"id": "0xXXxcXxv5PQqT$6i2zLgV",
"name": "xxx xxxxxxxx"
},
{
"id": "11Krt_our2rPCPqJ_2fKZR",
"name": "xxx xxxxxxxx xx"
},
{
"id": "2jYw4IyBP8KuozM_ej7DGf",
"name": "xxxxxxx 1"
},
{
"id": "3B8O805wL1ufabHMz1Je3v",
"name": "xxxxxxx 2"
},
{
"id": "0FVKUYZkvFaxd_OQUiyPBZ",
"name": "xxxxxxx"
},
{
"id": "3O41QFd0573QQvFco5zUUP",
"name": "Xxxxxxxxx"
}
],
"type": 0
}
],
"errorMessages": [],
"warningMessages": [],
"informationMessages": []
}
Model:
public class CFunctions
{
public int statusCode { get; set; }
public int version { get; set; }
public List<PFunctions>[] jsonData { get; set; }
public List<string> errorMessages { get; set; }
public List<string> warningMessages { get; set; }
public List<string> informationMessages { get; set; }
/*public CFunctions()
{
jsonData = new List<PFunctions>();
}*/
}
[Serializable]
public class PFunctions
{
public List<PAdditionalInfo>[] additionalInformation { get; set; }
public int type { get; set; }
/*public PFunctions()
{
additionalInformation = new List<PAdditionalInfo>();
}*/
}
[Serializable]
public class PAdditionalInfo
{
public Guid id { get; set; }
public string name { get; set; }
}
Deserialisation
var request = UnityWebRequest.Get(baseurl);
var operation = request.SendWebRequest();
var jsonResponse = request.downloadHandler.text;
List<CFunctions>[] PFunctionsList = JsonConvert.DeserializeObject<List<CFunctions>[]>(jsonResponse);
Error:
Cannot deserialize the current JSON object into type 'System.Collections.Generic.List`1[CFunctions][]' because the type requires a JSON array to deserialize correctly.
To fix this error either change the JSON to a JSON array or change the deserialized type so that it is a normal .NET type 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 'statusCode', line 1, position 14.
UnityEngine.Debug:Log(Object)
What I tried
The error pertains even when I changed List<PAdditionalInfo> to List<PAdditionalInfo>[]
I am not sure how to use JsonObjectAttribute and if it is the best way.
You've declared an array of List<T> in the models, eg List<PAdditionalInfo>[]. The json represents single arrays, not nested. You can fix that by choosing one or the other (I decided to use List<> but array is valid too):
public class PFunctions
{
public List<PAdditionalInfo> additionalInformation { get; set; } // removed []
...
}
public class CFunctions
{
public int statusCode { get; set; }
public int version { get; set; }
public List<PFunctions> jsonData { get; set; } // removed []
...
}
The class you're deserializing to is incorrect. Deserialize to the correct type (which is CFunctions not List<CFunctions>[]):
CFunctions cFunctions = JsonConvert.DeserializeObject<CFunctions>(json);
the most efficient way to get an additional information is this one line code and you only need one class
List<AdditionalInformation> additionalInformation = JObject.Parse(json)
["jsonData"][0]["additionalInformation"].ToObject<List<AdditionalInformation>>();
class
public class AdditionalInformation
{
public string id { get; set; }
public string name { get; set; }
}
I have a JSON snippet here taken from HttpClient class below in a C# .NET 5 program.
Simplified JSON:
{
"Restaurants":
[
{
"Id": 138898,
"Name": "Willesborough Cafe",
"Rating": {
"Count": 76,
"Average": 5.92,
"StarRating": 5.92
},
"CuisineTypes": [
{
"Id": 92,
"IsTopCuisine": false,
"Name": "Breakfast",
"SeoName": "breakfast"
}, {
"Id": 106,
"IsTopCuisine": true,
"Name": "British",
"SeoName": "british"
}
],
"Cuisines": [
{
"Name": "Breakfast",
"SeoName": "breakfast"
}, {
"Name": "British",
"SeoName": "british"
}
]
}
]
}
Current code:
dynamic result =
await _httpClient.GetFromJsonAsync<dynamic>(url);
// dynamic result2 = JsonConvert.DeserializeObject<dynamic>(result); // slow
dynamic result2 = JObject.Parse(result); // slow
I am interested to get the info from each restaurant below from the Restaurants array:
Name
Rating
CusineType
I use dynamic as I do not need to create multiple classes based on the JSON structure & I do not need to change my class if the JSON structure changes.
I have tried JsonConvert.DeserializeObject & JObject.Parse.
However, the Visual Studio debugging stuck at either of the method for a very long period
What is the recommended method to get partial properties from a huge JSON response?
Thanks
You can make a class with named properties
class Restaurant
{
public string Name { get; set; }
public Rating Rating { get; set; }
public List<CuisineType> CuisineTypes { get; set; }
}
class Rating
{
public int Count { get; set; }
public decimal Average { get; set; }
public decimal StarRating { get; set; }
}
class CuisineType
{
public int Id { get; set; }
public bool IsTopCuisine { get; set; }
public string Name { get; set; }
public string SeoName { get; set; }
}
and deserialize json to instance of Restaurant then you have a type you need. That's it.
You need to have a class contains list of Restaurant, because you must have a property equal name with your json object
class RestaurantList { public List<Restaurant> Restaurants {get; set;} }
Now you need a code to bind section of json to object
var restaurants = JsonConvert.DeserializeObject<RestaurantList>(result);
The idea is that I want to take my string, convert it to a JSON object, then loop through the results array to extract all of the id fields and put them in a separate array.
I've tried multiple 'C# string to JSON examples' but have gotten the farthest with this particular bit of code.
main:
String myString = "{
"total": 111,
"token": "abcdefghijklmn",
"results": [
{
"id": "001",
"some_stuff": {
"aValue": 0,
"bValue": 1
}
},
{
"id": "001",
"some_stuff": {
"aValue": 0,
"bValue": 1
}
},
{
"id": "001",
"some_stuff": {
"aValue": 0,
"bValue": 1
}
},
],
"whatdidido": {
"iretrieved": "yes"
}
}";
var list = JsonConvert.DeserializeObject<List<IdReturn>>(myString);
classes:
public class IdReturn
{
public int total { get; set; }
public string token { get; set; }
public List<Attribute> results { get; set; }
}
public class results
{
public string id { get; set; }
public string some_stuff { get; set; }
}
The expected result is a JSON object that I can use as: list.results[i].id to get to each id. The error message that I get from the code above is:
Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[myExample.IdReturn]' 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.
First your string is not written as valid C#, it should be:
String myString = #"{
""total"": 111,
""token"": ""abcdefghijklmn"",
""results"": [
{
""id"": ""001"",
""some_stuff"": {
""aValue"": 0,
""bValue"": 1
}
},
{
""id"": ""001"",
""some_stuff"": {
""aValue"": 0,
""bValue"": 1
}
},
{
""id"": ""001"",
""some_stuff"": {
""aValue"": 0,
""bValue"": 1
}
},
],
""whatdidido"": {
""iretrieved"": ""yes""
}
}";
Next you need a set of classes to represent the JSON structure:
public class Rootobject
{
public int total { get; set; }
public string token { get; set; }
public Result[] results { get; set; }
public Whatdidido whatdidido { get; set; }
}
public class Whatdidido
{
public string iretrieved { get; set; }
}
public class Result
{
public string id { get; set; }
public Some_Stuff some_stuff { get; set; }
}
public class Some_Stuff
{
public int aValue { get; set; }
public int bValue { get; set; }
}
And finally to deserialize it and print each id you can do this:
var root = JsonConvert.DeserializeObject<Rootobject>(myString);
Console.WriteLine(string.Join(",", root.results.Select(item => item.id)));
Which, for your sample will result in:
001,001,001
I launch this RestSharp query in JSON format:
var response = restClient.Execute<Report>(request);
The response I get contains this data
[
{
"Columns":
[
{"Name":"CameraGuid","Type":"Guid"},
{"Name":"ArchiveSourceGuid","Type":"Guid"},
{"Name":"StartTime","Type":"DateTime"},
{"Name":"EndTime","Type":"DateTime"},
{"Name":"TimeZone","Type":"String"},
{"Name":"Capabilities","Type":"UInt32"}
],
"Rows":
[
[
"00000001-0000-babe-0000-00408c71be50",
"3782fe37-6748-4d36-b258-49ed6a79cd6d",
"2013-11-27T17:52:00Z",
"2013-11-27T18:20:55.063Z",
"Eastern Standard Time",
2147483647
]
]
}
]
I'm trying to deserialize it into this group of classes:
public class Report
{
public List<ReportResult> Results { get; set; }
}
public class ReportResult
{
public List<ColumnField> Columns { get; set; }
public List<RowResult> Rows { get; set; }
}
public class ColumnField
{
public string Name { get; set; }
public string Type { get; set; }
}
public class RowResult
{
public List<string> Elements { get; set; }
}
Unfortunately, the result data is null and I get this exception:
Unable to cast object of type 'RestSharp.JsonArray' to type
'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
I cannot figure out what is wrong here.
I little help would be greatly appreciated.
Try this:
var response = restClient.Execute<List<ReportResult>>(request);
EDIT
You should also change ReportResult to:
public class ReportResult
{
public List<ColumnField> Columns { get; set; }
public List<List<string>> Rows { get; set; }
}
and you can get rid of Report and RowResult.
There is another way by creating wrapper class:
public class ThirdPartySuggesters : List<ThirdPartySuggester> {}
var response = client.Execute<ThirdPartySuggesters>(request);
My JSON feed has nested objects like this:
{
"id": 1765116,
"name": "StrozeR",
"birth": "2009-08-12",
"avatar": "http:\/\/static.erepublik.com\/uploads\/avatars\/Citizens\/2009\/08\/12\/f19db99e9baddad73981d214a6e576ef_100x100.jpg",
"online": true,
"alive": true,
"ban": null,
"level": 61,
"experience": 183920,
"strength": 25779.42,
"rank": {
"points": 133687587,
"level": 63,
"image": "http:\/\/www.erepublik.com\/images\/modules\/ranks\/god_of_war_1.png",
"name": "God of War*"
},
"elite_citizen": false,
"national_rank": 6,
"residence": {
"country": {
"id": 81,
"name": "Republic of China (Taiwan)",
"code": "TW"
},
"region": {
"id": 484,
"name": "Hokkaido"
}
}
}
and my object classes are like this:
class Citizen
{
public class Rank
{
public int points { get; set; }
public int level { get; set; }
public string image { get; set; }
public string name { get; set; }
}
public class RootObject
{
public int id { get; set; }
public string name { get; set; }
public string avatar { get; set; }
public bool online { get; set; }
public bool alive { get; set; }
public string ban { get; set; }
public string birth { get; set; }
public int level { get; set; }
public int experience { get; set; }
public double strength { get; set; }
public List<Rank> rank { get; set; }
}
}
I try to parse my JSON data with following code
private async void getJSON()
{
var http = new HttpClient();
http.MaxResponseContentBufferSize = Int32.MaxValue;
var response = await http.GetStringAsync(uri);
var rootObject = JsonConvert.DeserializeObject<Citizen.RootObject>(response);
uriTB.Text = rootObject.name;
responseDebug.Text = response;
}
but I get the following error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Erepublik.Citizen+Rank]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
I can't even parse the value in the main object. Anyway to fix this? and how can I parse a value inside of a nested object? for example: "points" in "rank"
Like the error message says, your rank property in the .NET class is a List<Rank>, but in your JSON it's just a nested object, not an array. Change it to just a Rank instead of a List<Rank>.
Arrays in JSON (or any Javascript, really) are enclosed in []. The {} characters specify a single object. The CLR type has to roughly match the JSON type in order to deserialize. Object to object, array to array.