List to JSON array C#
How to convert Json array to list of objects in c#As others have already pointed out, the reason you are not getting the results you expect is because your JSON does not match the class structure that you are trying to deserialize into. You either need to change your JSON or change your classes. Since others have already shown how to change the JSON, I will take the opposite approach here. To match the JSON you posted in your question, your classes should be defined like those below. Notice I've made the following changes:
Class definitions: class Wrapper { [JsonProperty("JsonValues")] public ValueSet ValueSet { get; set; } } class ValueSet { [JsonProperty("id")] public string Id { get; set; } [JsonProperty("values")] public DictionaryYou need to deserialize into the Wrapper class, not the ValueSet class. You can then get the ValueSet from the Wrapper. Here is a working program to demonstrate: class Program { static void Main(string[] args) { string jsonString = @" { ""JsonValues"": { ""id"": ""MyID"", ""values"": { ""value1"": { ""id"": ""100"", ""diaplayName"": ""MyValue1"" }, ""value2"": { ""id"": ""200"", ""diaplayName"": ""MyValue2"" } } } }"; var valueSet = JsonConvert.DeserializeObjectAnd here is the output: id: MyID value1 id: 100 value1 name: MyValue1 value2 id: 200 value2 name: MyValue2http://json2csharp.com/ I found the above link incredibly helpful as it corrected my C# classes by generating them from the JSON that was actually returned. Then I called : JsonConvert.DeserializeObjectand everything worked as expected. Did you check this line works perfectly & your string have value in it ? string jsonString = sr.ReadToEnd(); if yes, try this code for last line: ValueSet items = JsonConvert.DeserializeObject or if you have an array of json you can use list like this : List good luck |