Home > Enterprise >  Unable to Deserialize Json downloaded from a URL
Unable to Deserialize Json downloaded from a URL

Time:10-27

I am trying to deserialize the JSON downloaded from the following site downloaded as RawData

Json from the Site

but following error is being thrown

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type '' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

I tried using following methods

Root[] roots= JsonConvert.DeserializeObject<Root[]>(jsonString);

and

var roots = JsonConvert.DeserializeObject<List<Root>>(jsonString);

I used following class

public class Root
{
    public List<object> posts { get; set; }
    public List<Person> persons { get; set; }
    public List<Organization> organizations { get; set; }
    public Meta meta { get; set; }
    public List<Membership> memberships { get; set; }
    public List<Event> events { get; set; }
    public List<Area> areas { get; set; }
}
  

Following is the example JSON

"posts": [

  ],
  "persons": [
    {
      "birth_date": "1957-08-09",
      "contact_details": [
        {
          "type": "email",
          "value": "[email protected]"
        }
      ],
      "email": "[email protected]",
      "gender": "male",
      "id": "0094ff14-ff6c-440a-96fc-f0bd68068569",
      "identifiers": [
        {
          "identifier": "4655",
          "scheme": "everypolitician_legacy"
        },
        {
          "identifier": "hariomsinghrathore",
          "scheme": "prsindia"
        },
        {
          "identifier": "Q16897877",
          "scheme": "wikidata"
        }
      ],
      "image": "http://164.100.47.132/mpimage/photo/4655.jpg",
      "images": [
        {
          "url": "http://164.100.47.132/mpimage/photo/4655.jpg"
        }
      ],
      "links": [
        {
          "note": "Wikipedia (en)",
          "url": "https://en.wikipedia.org/wiki/Hariom_Singh_Rathore"
        }
      ],
      "name": "Yavatmal-Washim",
      "other_names": [
        {
          "lang": "en",
          "name": "Yavatmal-Washim Lok Sabha constituency",
          "note": "multilingual"
        },
        {
          "lang": "hi",
          "name": "यवतमाल-वाशिम लोक सभा निरà¥à¤µà¤¾à¤šà¤¨ कà¥à¤·à¥‡à¤¤à¥à¤°",
<?xml version="1.0" encoding="UTF-8"?> {
  "posts": [

  ],
  "persons": [
    {
      "birth_date": "1957-08-09",
      "contact_details": [
        {
          "type": "email",
          "value": "[email protected]"
        }
      ],
      "email": "[email protected]",
      "gender": "male",
      "id": "0094ff14-ff6c-440a-96fc-f0bd68068569",
      "identifiers": [
        {
          "identifier": "4655",
          "scheme": "everypolitician_legacy"
        },
        {
          "identifier": "hariomsinghrathore",
          "scheme": "prsindia"
        },
        {
          "identifier": "Q16897877",
          "scheme": "wikidata"
        }
      ],
      "image": "http://164.100.47.132/mpimage/photo/4655.jpg",
      "images": [
        {
          "url": "http://164.100.47.132/mpimage/photo/4655.jpg"
        }
      ],
      "links": [
        {
          "note": "Wikipedia (en)",
          "url": "https://en.wikipedia.org/wiki/Hariom_Singh_Rathore"
        }
      ],
      "name": "Rathore, Shri Hariom Singh",
      "other_names": [
        {
          "lang": "en",
          "name": "Hariom Singh Rathore",
          "note": "multilingual"
        },
        {
          "lang": "gu",
          "name": "હરિઓમ સિંહ રાઠૌડ઼",
          "note": "multilingual"
        }
        
      ]
    },
    {
      "birth_date": "1975-09-10",
      "contact_details": [
        {
          "type": "email",
          "value": "[email protected]"
        }
      ],
      "email": "[email protected]",
      "family_name": "Singh",
      "gender": "male",
      "id": "01727319-7f2b-465b-825c-1d7a94a54f70",
      "identifiers": [
        {
          "identifier": "4429",
          "scheme": "everypolitician_legacy"
        },
        {
          "identifier": "ravneetsingh",
          "scheme": "prsindia"
        },
        
      ],
      "image": "http://164.100.47.132/mpimage/photo/4429.jpg",
      "images": [
        {
          "url": "http://164.100.47.132/mpimage/photo/4429.jpg"
        }
      ],
      "links": [
        {
          "note": "Wikipedia (en)",
          "url": "https://en.wikipedia.org/wiki/Ravneet_Singh_Bittu"
        },
        {
          "note": "Wikipedia (pa)",
          "url": "https://pa.wikipedia.org/wiki/ਰਵਨੀਤ_ਸਿੰਘ"
        }
      ],
      "name": "Singh, Shri Ravneet",
      "other_names": [
          "note": "multilingual"
        },
        {
          "lang": "mr",
          "name": "यवतमाळ-वाशिम (लोकसभा मतदारसंघ)",
          "note": "multilingual"
        },
        {
          "lang": "ta",
        {
          "lang": "en",
          "name": "Ravneet Singh",
          "note": "multilingual"
        },
        
        {
          "lang": "te",
          "name": "రవనీతౠసింగౠబిటà±à°Ÿà±‚",
          "note": "multilingual"
        }
      ]
    },
    {
      "birth_date": "1958-09-07",
      "contact_details": [
        {
          "type": "email",
          "value": "[email protected]"
        }
      ],
      "email": "[email protected]",
      "family_name": "Mishra",
      "gender": "male",
      "id": "02670d6a-6b60-4e7b-b0cd-b4fc7d6c3bca",
      "identifiers": [
        {
          "identifier": "4626",
          "scheme": "everypolitician_legacy"
        },
        
        {
          "identifier": "Q16902096",
          "scheme": "wikidata"
        }
      ],
      "image": "http://164.100.47.132/mpimage/photo/4626.jpg",
      "images": [
        {
          "url": "http://164.100.47.132/mpimage/photo/4626.jpg"
        }
      ],
      "links": [
        {
          "note": "Wikipedia (en)",
          "url": "https://en.wikipedia.org/wiki/Bhairon_Prasad_Mishra"
        },
        {
          "note": "Wikipedia (hi)",
          "url": "https://hi.wikipedia.org/wiki/भैरों_पà¥à¤°à¤¸à¤¾à¤¦_मिशà¥à¤°"
        }
      ],
          "name": "யவதà¯à®®à®¾à®³à¯-வாசிம௠மகà¯à®•à®³à®µà¯ˆà®¤à¯ தொகà¯à®¤à®¿",
          "note": "multilingual"
        }
      ],
      "type": "constituency"
    },
    {
      "id": "zahirabad",
      "identifiers": [
        {
          "identifier": "Q8064692",
          "scheme": "wikidata"
        }
      ],
      "name": "Zahirabad",
      "other_names": [
        {
          "lang": "en",
          "name": "Zahirabad Lok Sabha constituency",
          "note": "multilingual"
        },
        {
          "lang": "hi",
          "name": "ज़हीराबाद लोक सभा निरà¥à¤µà¤¾à¤šà¤¨ कà¥à¤·à¥‡à¤¤à¥à¤° समà¥à¤ªà¤¾à¤¦à¤¨",
          "note": "multilingual"
        },
        {
          "lang": "mr",
          "name": "à¤à¤¹à¥€à¤°à¤¾à¤¬à¤¾à¤¦ (लोकसभा मतदारसंघ)",
          "note": "multilingual"
        },
        {
          "lang": "ta",
          "name": "ஜஹீராபாதà¯",
          "note": "multilingual"
        },
        {
          "lang": "te",
          "name": "జహీరాబాదౠలోకసభ నియోజకవరà±à°—à°‚",
          "note": "multilingual"
        }
      ],
      "type": "constituency"
    }
  ]
}

Can any one please help me in doing that. Thanks.

CodePudding user response:

Your json is not an array of roots. The json is a single object with multiple nested objects inside a single root.

{
 "posts": [
  ],
 "persons": [
  { "birth_date":
}

Since it's not returning an array of objects, you need to deserialize to a singular root.

var root = JsonConvert.DeserializeObject<Root>(jsonString);
  • Related