Please forgive the newbie question, but I'm having trouble deserializing a JSON collection and I all the examples I've found don't seem to work for my scenario.
When attempting to deserialize this file, I only get NULLs in return. Here's the file:
List<Element> jsonElements = new List<Element>();
//Get the list of elements
using (StreamReader file = File.OpenText(Server.MapPath(elementsDictionary)))
{
JsonSerializer serializer = new JsonSerializer();
jsonElements.Add((Element)serializer.Deserialize(file, typeof(Element)));
}
//Loop through each element, build the menu selector for it
foreach (var element in jsonElements)
{
//Do stuff here
}
public class Element
{
public string name { get; set; }
public string icon { get; set; }
public string content { get; set; }
}
Could anyone tell me what obvious dumb thing I'm doing wrong? I'd greatly greatly appreciate it.
The C# class does not match the JSON format. Use Visual Studio to create classes. Copy the JSON, then in Visual Studio click Edit -> Paste Special -> Paste JSON as Classes.
public class Element
{
public string name { get; set; }
public Item[] items { get; set; }
}
public class Item
{
public string name { get; set; }
public string icon { get; set; }
public string content { get; set; }
}
According to your description, I found that your json text file contains hierarchical relationships, and that's why you didn't get value in jsonElements.
In this case, you need to use the dynamictype to accept your deserialized data.
Here is the code:
List<Element> jsonElements = new List<Element>();
//Get the list of elements
using (StreamReader file = File.OpenText(Server.MapPath(elementsDictionary)))
{
dynamic stuff = JsonConvert.DeserializeObject(File.ReadAllText(Server.MapPath(elementsDictionary)));
for (int i = 0; i < stuff.elements[0].items.Count; i++)
{
Element element = new Element();
element.name = stuff.elements[0].items[i].name;
element.icon = stuff.elements[0].items[i].icon;
element.content = stuff.elements[0].items[i].content; jsonElements.Add(element);
}
}
Member
15 Points
74 Posts
How do I deserialize this JSON file?
Aug 16, 2019 04:12 PM|Piornet|LINK
Hi all,
Please forgive the newbie question, but I'm having trouble deserializing a JSON collection and I all the examples I've found don't seem to work for my scenario.
When attempting to deserialize this file, I only get NULLs in return. Here's the file:
And here's my code:
Could anyone tell me what obvious dumb thing I'm doing wrong? I'd greatly greatly appreciate it.
Thanks in advance!
All-Star
52261 Points
23315 Posts
Re: How do I deserialize this JSON file?
Aug 17, 2019 11:17 AM|mgebhard|LINK
The C# class does not match the JSON format. Use Visual Studio to create classes. Copy the JSON, then in Visual Studio click Edit -> Paste Special -> Paste JSON as Classes.
You're deserializing a collection.
Or
https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
Contributor
3710 Points
1043 Posts
Re: How do I deserialize this JSON file?
Aug 19, 2019 03:32 AM|Yongqing Yu|LINK
Hi Piornet.
According to your description, I found that your json text file contains hierarchical relationships, and that's why you didn't get value in jsonElements.
In this case, you need to use the dynamic type to accept your deserialized data.
Here is the code:
You can also refer to this link : Deserialize JSON into C# dynamic object?
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.