Answered by:
Cannot deserialize the current JSON object into type 'System.Collections.Generic.Lis

Question
-
User1242168447 posted
Hi Need some help please.
My Class object doesn't map to json while deserializing it.
using json2csharp.com and pasted my Json raw data in it to create class for deserializing object to, but it's generated many classes (RootObject and other called "__invalid_type__") and it's now confusing me... as I got many "__invalid_type__".
These are APIs this is endpoint to get the all
var stringJson = JsonConvert.DeserializeObject<List<RootObject>>(APIdatas);
Can you please assist?
public class __invalid_type__1 { public string name { get; set; } public string icon { get; set; } public string value { get; set; } } public class __invalid_type__2 { public string name { get; set; } public string icon { get; set; } public string value { get; set; } } public class __invalid_type__3 { public string name { get; set; } public string icon { get; set; } public string value { get; set; } } public class __invalid_type__4 { public string name { get; set; } public string icon { get; set; } public string value { get; set; } } public class ListAttributes { public __invalid_type__1 __invalid_name__1 { get; set; } public __invalid_type__2 __invalid_name__2 { get; set; } public __invalid_type__3 __invalid_name__3 { get; set; } public __invalid_type__4 __invalid_name__4 { get; set; } } public class RootObject { public int Id { get; set; } public string Title { get; set; } public string SubTitle { get; set; } public string Description { get; set; } public string InvestStrategy { get; set; } public string InvestType { get; set; } public string TargetReturn { get; set; } public int InvestTerm { get; set; } public string MinimalInvest { get; set; } public int SharePrice { get; set; } public int TotalShares { get; set; } public int AvailableShares { get; set; } public int BoughtShares { get; set; } public string InformationMemorandumUrl { get; set; } public string MainImage { get; set; } public List<string> Images { get; set; } public List<string> FloorPlan { get; set; } public ListAttributes ListAttributes { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public string Url { get; set; } }
Friday, October 18, 2019 6:57 PM
Answers
-
User475983607 posted
Use a dictionary for the ListAttributes.
public class InvestmentData { public int Id { get; set; } public string Title { get; set; } public string SubTitle { get; set; } public string Description { get; set; } public string InvestStrategy { get; set; } public string InvestType { get; set; } public string TargetReturn { get; set; } public int InvestTerm { get; set; } public string MinimalInvest { get; set; } public int SharePrice { get; set; } public int TotalShares { get; set; } public int AvailableShares { get; set; } public int BoughtShares { get; set; } public string InformationMemorandumUrl { get; set; } public string MainImage { get; set; } public string[] Images { get; set; } public string[] FloorPlan { get; set; } public Dictionary<string, ListAttribute> ListAttributes { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public string Url { get; set; } } public class ListAttribute { public string name { get; set; } public string icon { get; set; } public string value { get; set; } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 18, 2019 8:39 PM
All replies
-
User1577371250 posted
the second loop will give you RootObject. try below inside the foreach
var stringJson = JsonConvert.DeserializeObject<RootObject>(APIdatas);
Friday, October 18, 2019 7:57 PM -
User475983607 posted
Use a dictionary for the ListAttributes.
public class InvestmentData { public int Id { get; set; } public string Title { get; set; } public string SubTitle { get; set; } public string Description { get; set; } public string InvestStrategy { get; set; } public string InvestType { get; set; } public string TargetReturn { get; set; } public int InvestTerm { get; set; } public string MinimalInvest { get; set; } public int SharePrice { get; set; } public int TotalShares { get; set; } public int AvailableShares { get; set; } public int BoughtShares { get; set; } public string InformationMemorandumUrl { get; set; } public string MainImage { get; set; } public string[] Images { get; set; } public string[] FloorPlan { get; set; } public Dictionary<string, ListAttribute> ListAttributes { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public string Url { get; set; } } public class ListAttribute { public string name { get; set; } public string icon { get; set; } public string value { get; set; } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 18, 2019 8:39 PM -
User1120430333 posted
I had no problem doing this.
namespace Entities { public class DtoCacheRoot { public DtoCache DtoCache { get; set; } } } ========================================================== using System.Collections.Generic; namespace Entities { public class DtoCache { public List<DtoProjectType> ProjectTypes { get; set; } = new List<DtoProjectType>(); public List<DtoStatus> Statuses { get; set; } = new List<DtoStatus>(); public List<DtoResource> Resources { get; set; } = new List<DtoResource>(); public List<DtoDuration> Durations { get; set; } = new List<DtoDuration>(); } } ===================================================================== public DtoCache GetCacheApi() { var dtocache = new DtoCache(); using (var client = new HttpClient()) { var uri = new Uri("http://progmgmntcore2api.com/api/cache"); var response = client.GetAsync(uri).Result; if (!response.IsSuccessStatusCode) throw new Exception(response.ToString()); var responseContent = response.Content; var responseString = responseContent.ReadAsStringAsync().Result; var deserialized = JsonConvert.DeserializeObject<DtoCacheRoot>(responseString); dtocache.ProjectTypes = deserialized.DtoCache.ProjectTypes; dtocache.Durations = deserialized.DtoCache.Durations; dtocache.Statuses = deserialized.DtoCache.Statuses; dtocache.Resources = deserialized.DtoCache.Resources; } return dtocache; }
Saturday, October 19, 2019 10:52 AM -
User475983607 posted
@Da924, your response is not related to the problem. This URL https://www.crowdwithus.london/api/endpoint/38FF9517A0E955C14A84336919F507DA1785E77E2EA79335A3930E59E3F3B7CD/deals/31 returns JSON formatted as.
{ "ListAttributes": { "1": { "name": "Second Charge Security", "icon": "fa-check-square-o", "value": "" }, "2": { "name": "", "icon": "fa-check-square-o", "value": "" }, "3": { "name": "Second Ranking Debenture", "icon": "fa-check-square-o", "value": "" }, "4": { "name": "Personal Guarantees", "icon": "fa-check-square-o", "value": "" } } }
The code generator does not recognize the numbers 1,2,3,4 as a dictionary.
public Dictionary<int, ListAttribute> ListAttributes { get; set; }
Simply updating the model fixes this issue.
Saturday, October 19, 2019 11:01 AM