Get exception while deserializing JSON - c#

I'm having a problem deserializing a JSON file to an object, it's returning a JSON exception, here's my code:
class Program
{
static void Main(string[] args)
{
LoadJson(filePath);
}
public static void LoadJson(string filePath)
{
using (StreamReader r = new StreamReader(filePath))
{
string json = r.ReadToEnd();
List<Rootobject> items = JsonConvert.DeserializeObject<List<Rootobject>>(json);
}
}
}
public class Rootobject
{
public int total { get; set; }
public Value[] values { get; set; }
}
public class Value
{
public string name { get; set; }
public int count { get; set; }
public int previous_count { get; set; }
}
Here's the JSON file:
{
"total": 520738,
"values": [
{
"name": "2",
"count": 311326,
"previous_count": 11224
},
{
"name": "3",
"count": 73749,
"previous_count": 2747
},
{
"name": "4",
"count": 47810,
"previous_count": 1830
},
{
"name": "6",
"count": 20414,
"previous_count": 759
},
{
"name": "5",
"count": 14481,
"previous_count": 596
},
{
"name": "8",
"count": 7258,
"previous_count": 233
},
{
"name": "7",
"count": 6452,
"previous_count": 286
},
{
"name": "9",
"count": 4439,
"previous_count": 167
},
{
"name": "10",
"count": 4273,
"previous_count": 145
},
{
"name": "12",
"count": 2969,
"previous_count": 82
},
{
"name": "11",
"count": 1890,
"previous_count": 65
},
{
"name": "15",
"count": 1279,
"previous_count": 69
},
{
"name": "16",
"count": 1109,
"previous_count": 36
},
{
"name": "1",
"count": 1021,
"previous_count": 448
},
{
"name": "20",
"count": 906,
"previous_count": 68
},
{
"name": "18",
"count": 867,
"previous_count": 14
},
{
"name": "14",
"count": 844,
"previous_count": 58
},
{
"name": "13",
"count": 380,
"previous_count": 47
},
{
"name": "30",
"count": 376,
"previous_count": 9
},
{
"name": "40",
"count": 296,
"previous_count": 8
},
{
"name": "25",
"count": 282,
"previous_count": 2
},
{
"name": "17",
"count": 224,
"previous_count": 14
},
{
"name": "24",
"count": 188,
"previous_count": 23
},
{
"name": "23",
"count": 165,
"previous_count": 1
},
{
"name": "32",
"count": 157,
"previous_count": 0
},
{
"name": "28",
"count": 71,
"previous_count": 8
},
{
"name": "60",
"count": 48,
"previous_count": 2
},
{
"name": "39",
"count": 39,
"previous_count": 2
},
{
"name": "46",
"count": 29,
"previous_count": 0
},
{
"name": "22",
"count": 27,
"previous_count": 5
},
{
"name": "90",
"count": 24,
"previous_count": 0
},
{
"name": "21",
"count": 23,
"previous_count": 11
},
{
"name": "19",
"count": 20,
"previous_count": 0
},
{
"name": "26",
"count": 17,
"previous_count": 1
},
{
"name": "41",
"count": 16,
"previous_count": 0
},
{
"name": "33",
"count": 13,
"previous_count": 0
},
{
"name": "34",
"count": 10,
"previous_count": 4
},
{
"name": "35",
"count": 10,
"previous_count": 0
},
{
"name": "27",
"count": 9,
"previous_count": 4
},
{
"name": "43",
"count": 8,
"previous_count": 0
},
{
"name": "38",
"count": 8,
"previous_count": 0
},
{
"name": "81",
"count": 7,
"previous_count": 0
},
{
"name": "61",
"count": 6,
"previous_count": 0
},
{
"name": "76",
"count": 3,
"previous_count": 0
},
{
"name": "48",
"count": 3,
"previous_count": 3
},
{
"name": "140",
"count": 2,
"previous_count": 2
},
{
"name": "51",
"count": 2,
"previous_count": 0
},
{
"name": "55",
"count": 2,
"previous_count": 0
},
{
"name": "162",
"count": 1,
"previous_count": 0
},
{
"name": "120",
"count": 1,
"previous_count": 0
},
{
"name": "170",
"count": 1,
"previous_count": 1
},
{
"name": "145",
"count": 1,
"previous_count": 0
},
{
"name": "160",
"count": 1,
"previous_count": 0
},
{
"name": "29",
"count": 1,
"previous_count": 3
},
{
"name": "104",
"count": 1,
"previous_count": 0
},
{
"name": "235",
"count": 1,
"previous_count": 0
},
{
"name": "110",
"count": 1,
"previous_count": 0
},
{
"name": "42",
"count": 1,
"previous_count": 15
},
{
"name": "245",
"count": 1,
"previous_count": 0
},
{
"name": "275",
"count": 1,
"previous_count": 0
},
{
"name": "288",
"count": 1,
"previous_count": 0
},
{
"name": "Other",
"count": 17173,
"previous_count": 934
}
]
}
Tried changing the Value class to a List but still had the same issue. This is the returned exception:
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 'total', line 2, position 12.
Thanks!

Your attached JSON is an object with Rootobject type, but not an array.
Deserialize as Rootobject.
Rootobject item = JsonConvert.DeserializeObject<Rootobject>(json);
While in naming conventions, we recommend using Pascal Casing for public members.
Modify all your properties to Pascal Case and apply the [JsonProperty] attribute to map the name of properties in JSON.
With [JsonProperty] attribute
public class Rootobject
{
[JsonProperty("total")]
public int Total { get; set; }
[JsonProperty("values")]
public Value[] Values { get; set; }
}
public class Value
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("count")]
public int Count { get; set; }
[JsonProperty("previous_count")]
public int PreviousCount { get; set; }
}
Or With CamelCasePropertyNamesContractResolver
Rootobject item = JsonConvert.DeserializeObject<Rootobject>(json, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
Demo # .NET Fiddle

Related

Group list by nested list and remove elements not corresponding to group

Let's say I have such structure (but both A and B has a lot more properties):
class C
{
List<A> A { get; set; }
}
class A
{
int Id { get; set; }
string Name { get; set; }
List<B> B { get; set; }
}
class B
{
int Id { get; set; }
string Name { get; set; }
int GroupId { get; set; }
}
And I would like to group by B.GroupId and also keep only those elements in each group that has corresponding GroupId value.
Here is an example data:
[
{
"Id": 1,
"Name": "A1",
"B": [
{
"Id": 1,
"Name": "B1",
"GroupId": 1
},
{
"Id": 2,
"Name": "B2",
"GroupId": 1
},
{
"Id": 3,
"Name": "B3",
"GroupId": 2
}
]
},
{
"Id": 2,
"Name": "A2",
"B": [
{
"Id": 4,
"Name": "B4",
"GroupId": 1
},
{
"Id": 5,
"Name": "B5",
"GroupId": 2
},
{
"Id": 6,
"Name": "B6",
"GroupId": 3
}
]
}
]
And as a resolut I would like to have 3 groups:
Group 1:
[
{
"Id": 1,
"Name": "A1",
"B": [
{
"Id": 1,
"Name": "B1",
"GroupId": 1
},
{
"Id": 2,
"Name": "B2",
"GroupId": 1
}
]
},
{
"Id": 2,
"Name": "A2",
"B": [
{
"Id": 4,
"Name": "B4",
"GroupId": 1
}
]
}
]
Group 2:
[
{
"Id": 1,
"Name": "A1",
"B": [
{
"Id": 3,
"Name": "B3",
"GroupId": 2
}
]
},
{
"Id": 2,
"Name": "A2",
"B": [
{
"Id": 5,
"Name": "B5",
"GroupId": 2
}
]
}
]
Group 3:
[
{
"Id": 2,
"Name": "A2",
"B": [
{
"Id": 6,
"Name": "B6",
"GroupId": 3
}
]
}
]
I have found that I can group by B.GroupId like this:
from a in c
from b in a.B
group a by b.GroupId
But how do I get rid of those elements whose GroupId is not in corresponding group? I tried removing them in foreach loop, but that removes them from all the groups not just one.
you can try using Linq. Here is the example for 'GroupBy'
var jsonstring = File.ReadAllText("json1.json");
var objRoot = JsonConvert.DeserializeObject<List<A>>(jsonstring);
var result = objRoot.SelectMany(x => x.B)
.GroupBy(x => x.GroupId)
.ToDictionary(x => x.Key, y => y.ToArray());
EDIT
var jsonstring = File.ReadAllText("json1.json");
var objRoot = JsonConvert.DeserializeObject<List<A>>(jsonstring);
var result = objRoot.SelectMany(AObj => AObj.B, (AObj, B) => new { AObj, B })
.GroupBy(BObj => BObj.B.GroupId)
.ToDictionary(x => x.Key, y => y.Select(y =>
new A
{
Id = y.AObj.Id,
Name = y.AObj.Name,
B = new List<B> { y.B }
}).GroupBy(x => x.Id).ToList());
Console.WriteLine(JsonConvert.SerializeObject(result));

Filtering out array items in a CosmosDB query with best performance

In CosmosDB I am able to select documents where items in an array have a given value using ARRAY_CONTAINS. For example:
SELECT * FROM d WHERE ARRAY_CONTAINS(d.Assignments, {'Owner':'Jason'}, true)
In the above query I get the following returned:
[
{
"id": "0",
"Assignments": [
{
"Fruit": "Apple",
"Owner": "Jason"
},
{
"Fruit": "Orange",
"Owner": "Jason"
},
{
"Fruit": "Pear",
"Owner": "Amy"
}
]
},
{
"id": "1",
"Assignments": [
{
"Fruit": "Pear",
"Owner": "Liz"
},
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
},
{
"id": "2",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Liz"
},
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
}
]
However I would also like the returned JSON to have all array items that do not match my query to be filtered out. For example:
[
{
"id": "0",
"Assignments": [
{
"Fruit": "Apple",
"Owner": "Jason"
},
{
"Fruit": "Orange",
"Owner": "Jason"
}
]
},
{
"id": "1",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
},
{
"id": "2",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
}
]
I would prefer to find a way to do this in my query assuming I can do so with good performance and relatively low Request Units.
Is it more advisable to filter out the results in code once the JSON is returned?
In some scenarios I may have a few hundred array items with about 60-80% needing to be filtered out.
I added 3 more similar documents for these records. You can use below query to fulfill this requirement in an optimum way:
SELECT f.id, ARRAY(SELECT * FROM c in f.Assignments WHERE c.Owner = 'Jason') AS Assignments FROM f WHERE ARRAY_CONTAINS(f.Assignments, {'Owner':'Jason'}, true)
Results:
[
{
"id": "0",
"Assignments": [
{
"Fruit": "Apple",
"Owner": "Jason"
},
{
"Fruit": "Orange",
"Owner": "Jason"
}
]
},
{
"id": "1",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
},
{
"id": "2",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
},
{
"id": "3",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
},
{
"id": "4",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
},
{
"id": "5",
"Assignments": [
{
"Fruit": "Grape",
"Owner": "Jason"
}
]
}
]
Query Stats:

Entity Framework Core self referencing entity JSON response

I have a .Net Core 3.0 Web API project and I'm using Entity Framework as my ORM. I have a self referencing entity that looks like this:
public partial class Category
{
public Category()
{
Events = new HashSet<Event>();
InverseParentCategory = new HashSet<Category>();
}
public int Id { get; set; }
public string Name { get; set; }
public int? ParentCategoryId { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual ICollection<Event> Events { get; set; }
public virtual ICollection<Category> InverseParentCategory { get; set; }
}
And this is the action method in my CategoriesController that returns all the categories as a JSON object:
[HttpGet]
public async Task<IActionResult> GetAllCategories()
{
var allCategories = await _categoryService.GetCategories();
return Ok(allCategories);
}
And this is my GetCategories method in my CategoryService:
public async Task<IEnumerable<Category>> GetCategories()
{
var allCategories = await _context.Categories.ToListAsync();
return allCategories;
}
The response that I get through Postman is this:
[
{
"id": 1,
"name": "Soccer",
"parentCategoryId": null,
"parentCategory": null,
"events": [],
"inverseParentCategory": [
{
"id": 3,
"name": "Basketbal",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 4,
"name": "Volleyball",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 5,
"name": "",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 6,
"name": "dada",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
}
]
},
{
"id": 2,
"name": "Basketbal",
"parentCategoryId": null,
"parentCategory": null,
"events": [],
"inverseParentCategory": []
},
{
"id": 3,
"name": "Basketbal",
"parentCategoryId": 1,
"parentCategory": {
"id": 1,
"name": "Soccer",
"parentCategoryId": null,
"parentCategory": null,
"events": [],
"inverseParentCategory": [
{
"id": 4,
"name": "Volleyball",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 5,
"name": "",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 6,
"name": "dada",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
}
]
},
"events": [],
"inverseParentCategory": []
},
{
"id": 4,
"name": "Volleyball",
"parentCategoryId": 1,
"parentCategory": {
"id": 1,
"name": "Soccer",
"parentCategoryId": null,
"parentCategory": null,
"events": [],
"inverseParentCategory": [
{
"id": 3,
"name": "Basketbal",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 5,
"name": "",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 6,
"name": "dada",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
}
]
},
"events": [],
"inverseParentCategory": []
},
{
"id": 5,
"name": "",
"parentCategoryId": 1,
"parentCategory": {
"id": 1,
"name": "Soccer",
"parentCategoryId": null,
"parentCategory": null,
"events": [],
"inverseParentCategory": [
{
"id": 3,
"name": "Basketbal",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 4,
"name": "Volleyball",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 6,
"name": "dada",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
}
]
},
"events": [],
"inverseParentCategory": []
},
{
"id": 6,
"name": "dada",
"parentCategoryId": 1,
"parentCategory": {
"id": 1,
"name": "Soccer",
"parentCategoryId": null,
"parentCategory": null,
"events": [],
"inverseParentCategory": [
{
"id": 3,
"name": "Basketbal",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 4,
"name": "Volleyball",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
},
{
"id": 5,
"name": "",
"parentCategoryId": 1,
"events": [],
"inverseParentCategory": []
}
]
},
"events": [],
"inverseParentCategory": []
}
]
So you can see that when a child has a parent it return the parent's data and its children. I want to avoid this but I can't actually think of anything. Can I get any help?

Deserializing complex object using Json.NET

I need to deserialize the this json returned from grogle maps api:
{
"destination_addresses": [
"Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia",
"Via Torino, 20123 Milano, Italia",
"Via Guglielmo Marconi, 71, 40121 Bologna, Italia",
"Via Irnerio, 40126 Bologna, Italia"
],
"origin_addresses": [
"Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia",
"Via Torino, 20123 Milano, Italia",
"Via Guglielmo Marconi, 71, 40121 Bologna, Italia",
"Via Irnerio, 40126 Bologna, Italia"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "1 m",
"value": 0
},
"duration": {
"text": "1 min",
"value": 0
},
"status": "OK"
},
{
"distance": {
"text": "286 km",
"value": 286281
},
"duration": {
"text": "2 ore 48 min",
"value": 10083
},
"status": "OK"
},
{
"distance": {
"text": "80,1 km",
"value": 80088
},
"duration": {
"text": "1 ora 3 min",
"value": 3789
},
"status": "OK"
},
{
"distance": {
"text": "77,6 km",
"value": 77594
},
"duration": {
"text": "57 min",
"value": 3422
},
"status": "OK"
}
]
},
{
"elements": [
{
"distance": {
"text": "288 km",
"value": 287811
},
"duration": {
"text": "2 ore 48 min",
"value": 10052
},
"status": "OK"
},
{
"distance": {
"text": "1 m",
"value": 0
},
"duration": {
"text": "1 min",
"value": 0
},
"status": "OK"
},
{
"distance": {
"text": "212 km",
"value": 212423
},
"duration": {
"text": "2 ore 8 min",
"value": 7664
},
"status": "OK"
},
{
"distance": {
"text": "218 km",
"value": 218219
},
"duration": {
"text": "2 ore 9 min",
"value": 7740
},
"status": "OK"
}
]
},
{
"elements": [
{
"distance": {
"text": "78,5 km",
"value": 78528
},
"duration": {
"text": "56 min",
"value": 3346
},
"status": "OK"
},
{
"distance": {
"text": "212 km",
"value": 212190
},
"duration": {
"text": "2 ore 5 min",
"value": 7519
},
"status": "OK"
},
{
"distance": {
"text": "1 m",
"value": 0
},
"duration": {
"text": "1 min",
"value": 0
},
"status": "OK"
},
{
"distance": {
"text": "2,0 km",
"value": 1979
},
"duration": {
"text": "5 min",
"value": 316
},
"status": "OK"
}
]
},
{
"elements": [
{
"distance": {
"text": "74,7 km",
"value": 74719
},
"duration": {
"text": "55 min",
"value": 3278
},
"status": "OK"
},
{
"distance": {
"text": "218 km",
"value": 217951
},
"duration": {
"text": "2 ore 9 min",
"value": 7712
},
"status": "OK"
},
{
"distance": {
"text": "3,8 km",
"value": 3782
},
"duration": {
"text": "11 min",
"value": 671
},
"status": "OK"
},
{
"distance": {
"text": "1 m",
"value": 0
},
"duration": {
"text": "1 min",
"value": 0
},
"status": "OK"
}
]
}
],
"status": "OK"
}
I need to create a Distance Matrix so I'm only interested in the "value" field inside "distance".
I've tried this approach:
DataSet data = JsonConvert.DeserializeObject<DataSet>(jsonResponse);
DataTable dataTab = data.Tables["Elements"];
foreach (DataRow elements in dataTab.Rows)
{
Console.WriteLine(elements["distance"]);
//Do something else here
}
But The JSonConvert returns "Additional text found in JSON string after finishing deserializing object."
You should deserialize to classes that match your data. You can generate these classes at http://json2csharp.com/.
// use like
var rootObj = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
foreach (var row in rootObj.rows)
{
foreach (var element in row.elements)
{
Console.WriteLine(element.distance.text);
}
}
// you might want to change the property names to .Net conventions
// use [JsonProperty] to let the serializer know the JSON names where needed
public class Distance
{
public string text { get; set; }
public int value { get; set; }
}
public class Duration
{
public string text { get; set; }
public int value { get; set; }
}
public class Element
{
public Distance distance { get; set; }
public Duration duration { get; set; }
public string status { get; set; }
}
public class Row
{
public List<Element> elements { get; set; }
}
public class RootObject
{
public List<string> destination_addresses { get; set; }
public List<string> origin_addresses { get; set; }
public List<Row> rows { get; set; }
public string status { get; set; }
}
I believe the issue is with the cast to 'DataSet' based on a similar question I found searching Stack Overflow.
Try this as I believe it would work (you may need to use 'rows' instead of 'Elements', but I believe the approach of using a JObject will resolve the primary issue of the 'additional text'.
JObject json = JsonConvert.DeserializeObject<JObject>(jsonResponse);
foreach (Dictionary<string, object> item in data["Elements"])
{
foreach (string val in item.Values) {
Console.WriteLine(val);
}
}
Using dynamic :
dynamic json = JsonConvert.DeserializeObject(jsonResponse);
var rowCount = json.rows.Count;
Func<dynamic, int> getElementCount = r => r.elements.Count;
var maxElements = Enumerable.Max(json.rows, getElementCount);
var matrix = new int?[rowCount, maxElements];
for(int i = 0; i < rowCount; i++)
{
var elements = json.rows[i].elements;
for(int j = 0; j < elements.Count; j++)
{
var element = elements[j];
matrix[i, j] = element.distance.value;
}
}

Cannot deserialize JSON object into type 'System.Collections.Generic.List'

i'm trying to deserialize a json string pulled from the web using json.net, but i am getting a Cannot deserialize JSON object error. Here is the json string
{
"metadata": {
"page": 1,
"perPage": 23,
"count": 23
},
"results": [
{
"id": 9,
"name": "Breaks",
"slug": "breaks",
"subgenres": []
},
{
"id": 10,
"name": "Chill Out",
"slug": "chill-out",
"subgenres": []
},
{
"id": 12,
"name": "Deep House",
"slug": "deep-house",
"subgenres": []
},
{
"id": 16,
"name": "DJ Tools",
"slug": "dj-tools",
"subgenres": []
},
{
"id": 1,
"name": "Drum & Bass",
"slug": "drum-and-bass",
"subgenres": []
},
{
"id": 18,
"name": "Dubstep",
"slug": "dubstep",
"subgenres": []
},
{
"id": 17,
"name": "Electro House",
"slug": "electro-house",
"subgenres": []
},
{
"id": 3,
"name": "Electronica",
"slug": "electronica",
"subgenres": []
},
{
"id": 40,
"name": "Funk / R&B",
"slug": "funk-r-and-b",
"subgenres": []
},
{
"id": 49,
"name": "Glitch Hop",
"slug": "glitch-hop",
"subgenres": []
},
{
"id": 8,
"name": "Hard Dance",
"slug": "hard-dance",
"subgenres": []
},
{
"id": 2,
"name": "Hardcore / Hard Techno",
"slug": "hardcore-hard-techno",
"subgenres": []
},
{
"id": 38,
"name": "Hip-Hop",
"slug": "hip-hop",
"subgenres": []
},
{
"id": 5,
"name": "House",
"slug": "house",
"subgenres": []
},
{
"id": 37,
"name": "Indie Dance / Nu Disco",
"slug": "indie-dance-nu-disco",
"subgenres": []
},
{
"id": 14,
"name": "Minimal",
"slug": "minimal",
"subgenres": []
},
{
"id": 39,
"name": "Pop / Rock",
"slug": "pop-rock",
"subgenres": []
},
{
"id": 15,
"name": "Progressive House",
"slug": "progressive-house",
"subgenres": []
},
{
"id": 13,
"name": "Psy-Trance",
"slug": "psy-trance",
"subgenres": []
},
{
"id": 41,
"name": "Reggae / Dub",
"slug": "reggae-dub",
"subgenres": []
},
{
"id": 11,
"name": "Tech House",
"slug": "tech-house",
"subgenres": []
},
{
"id": 6,
"name": "Techno",
"slug": "techno",
"subgenres": []
},
{
"id": 7,
"name": "Trance",
"slug": "trance",
"subgenres": []
}
]
}
And my classes
public class Genres
{
public Metadata metadata { get; set; }
public List<Result> results { get; set; }
}
public class Metadata
{
public int page {get; set; }
public int perPage { get; set;}
public int count { get; set; }
}
public class Result
{
public int id { get; set; }
public string name { get; set; }
public string slug { get; set; }
public List<object> subgenres { get; set; }
}
And my code to deserialize the data using json.net.
void beatportTest_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
List<Result> data = JsonConvert.DeserializeObject<List<Result>>(e.Result);
//foreach loop to display data
I want to be able to display the name of each genre from the Results class, but i get the error when my app compiles.
Your JSON data has two main elements metadata and results. And according to you class structure, the Genres class also has the same structure. But in your code you are trying to de-serialize the structure to Results, thats why you are getting the error.You should try to de-serialize to Genres class.The new code will be something like
void beatportTest_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
Genres data = JsonConvert.DeserializeObject(e.Result);
// for-each loop to display data
}
catch(Exception ex)
{
}
}

Categories

Resources