1

From a C# program I am calling an RestFul API in an external system.

The calling finally is done with this code:

string response = await cliente.GetDataByPost($"/index.php?route=api/catalog/products&token={authentication.api_token}", null, null);
List<Models.Producto> productos = JsonConvert.DeserializeObject<List<Models.Productos>>(response);

response variable coming from the external system is the json string shown in the following image:

enter image description here

Under 42, 30, 47, and so on, are the ordinary product objects (the same number as the ID). That object has several properties, so for the case now, it is not important to post here. I can only say that the object is the Models.Producto entity.

Of course, that DeserializeObject call returns an error.

I call this json string a weird object because in order to deserialize using normal deserialization procedure, those numbers (42, 30, 47, etc.) should be properties of products object.

How can I configure JsonConvert so that I can deserialize this thing using the following entity models? Is this possible?

public class Productos
{
    public Success success { get; set; }
}

public class Success
{
    public List<Systems.Models.Producto> products { get; set; }
}
}
jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • Have you tried json2csharp.com? – Daniel Pavlovsky Apr 17 '21 at 01:12
  • 4
    That looks like it might be a `Dictionary` rather than a `List` – CoolBots Apr 17 '21 at 01:17
  • @DanielPavlovsky you can also just "Paste JSON as Classes" in Visual Studio – CoolBots Apr 17 '21 at 01:19
  • @DanielPavlovsky I did not know that site. I tried it, and it created several classes `public class _42`, `public class _41`, `public class _40`, and so on.... – jstuardo Apr 17 '21 at 01:21
  • 3
    @CoolBots Thanks.... it worked! it seems that the easiest things are the more difficult to resolve sometimes, hehehe – jstuardo Apr 17 '21 at 01:27
  • By the way, a better way to show JSON (which is a text-based format) is as text. Use the debugger's text visualizer rather than the JSIN visualizer. – Flydog57 Apr 17 '21 at 04:03
  • Might you please share the actual JSON **as text**, not as a screen shot? For instance, with that visualizer it's almost impossible to distinguish between a JSON array and a JSON object with numeric keys. A [mcve] would be ideal, and maximize the chance we can help. See: [ask]. – dbc Apr 17 '21 at 15:32
  • @dbc the json is extremely big, so, for this case, it is more illustrating to show the screenshot... and if you have read in comments, someone has already given me the clue. Thanks anyway. – jstuardo Apr 18 '21 at 03:45

0 Answers0