0

I have a JSON file which looks like this

{
  "Response": {
    "Part": {
      "Name": "Part1",
      "Rev": "01",
      "Title": "Part1",
      "current": "Released",
      "Part": [
        {
          "Name": "Part2",
          "Rev": "00",
          "Title": "Part2",
          "current": "Released",
          "Part": {
            "Name": "Part3",
            "Rev": "R00",
            "Title": "Part3",
            "current": "Released"
          }
        },
        {
          "Name": "Part4",
          "Rev": "00",
          "Title": "Part4",
          "current": "Released"
        }
      ]
    }
  }
}

I have created my class objects as this

public class PartObj
{
    public string Name { get; set; }
    public string Rev { get; set; }
    public string Title { get; set; }
    public string current { get; set; }
    public List<PartObj> Part { get; set; }
}

public class Response
{
    public PartObj Part { get; set; }
}

public class Root
{
    public Response Response { get; set; }
}

But I am unable to deserialize the JSON string

Root items = JsonConvert.DeserializeObject<Root>(jsonStr);

The error says

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[PartObj ]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Any Solution for Deserializing this?

Myth3791
  • 1
  • 2
  • `part` doesn't always come as a array from json – Okan Karadag Jul 07 '22 at 13:09
  • 1
    as the error says `Part` should be an array but in your case, it's `object`. Look middle of your json – Maruf Jul 07 '22 at 13:10
  • Looks like you need to swap `public PartObj Part { get; set; }` and `public List Part { get; set; }` – Charlieface Jul 07 '22 at 13:11
  • I Changed `public List Part { get; set; }` to `public List Part { get; set; }` And I don't get the error. Now I will have to access the individual items and have to check. – Myth3791 Jul 07 '22 at 13:53

1 Answers1

0

'Part' : member names cannot be the same as their enclosing type

This is why its happening:

The members of a class or struct cannot have the same name as the class or struct.

Try the following approach it should work just fine.

public class Root
 {
   public Response Response { get; set; }
 }

 public class Response
 {
    public Part Part { get; set; }
 }

public class Part
 {
   public string Name { get; set; }
   public string Rev { get; set; }
   public string Title { get; set; }
   public string current { get; set; }
   public List<Part> Parts { get; set; }   
}
Sydney_dev
  • 1,448
  • 2
  • 17
  • 24
  • I changed the names as mentioned, but still no luck. Changing public List Part { get; set; } to public List Part { get; set; } helped to at least see the data – Myth3791 Jul 07 '22 at 14:01
  • What error do you get? – Sydney_dev Jul 07 '22 at 15:30
  • I have posted the error in the question. As @Maruf replied, `Part` sometimes comes as an array and sometimes as an object. Thats why there is the issue to deserialize. If I put it as object, then it seems to be working. – Myth3791 Jul 08 '22 at 04:48