-2

I have this restcountries url https://restcountries.eu/rest/v2/all

I want to consume in C#

string URL = "https://restcountries.eu/rest/v2/all";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.ContentType = "application/json; charset=utf-8";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (Stream responseStream = response.GetResponseStream())
{
    StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();                                                       
    String jsonstr = reader.ReadToEnd();                               
    RootObject obj = serializer.Deserialize<RootObject>(reader.ReadToEnd());
    RootObject robj = serializer.Deserialize<RootObject>(jsonstr);
    //  System.Diagnostics.Debug.WriteLine(reader.ReadToEnd());
    // Console.WriteLine(reader.ReadToEnd());
}

Classes being deserialized:

public class Currency
{
    public string code { get; set; }
    public string name { get; set; }
    public string symbol { get; set; }
}

public class Language
{
    public string iso639_1 { get; set; }
    public string iso639_2 { get; set; }
    public string name { get; set; }
    public string nativeName { get; set; }
}

public class Translations
{
    public string de { get; set; }
    public string es { get; set; }
    public string fr { get; set; }
    public string ja { get; set; }
    public string it { get; set; }
    public string br { get; set; }
    public string pt { get; set; }
    public string nl { get; set; }
    public string hr { get; set; }
    public string fa { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public List<string> topLevelDomain { get; set; }
    public string alpha2Code { get; set; }
    public string alpha3Code { get; set; }
    public List<string> callingCodes { get; set; }
    public string capital { get; set; }
    public List<object> altSpellings { get; set; }
    public string region { get; set; }
    public string subregion { get; set; }
    public int population { get; set; }
    public List<object> latlng { get; set; }
    public string demonym { get; set; }
    public double? area { get; set; }
    public double? gini { get; set; }
    public List<string> timezones { get; set; }
    public List<object> borders { get; set; }
    public string nativeName { get; set; }
    public string numericCode { get; set; }
    public List<Currency> currencies { get; set; }
    public List<Language> languages { get; set; }
    public Translations translations { get; set; }
    public string flag { get; set; }
    public List<object> regionalBlocs { get; set; }
    public string cioc { get; set; }
}

When I run the no deserialized data coming. only null returned. what is the problem, can anyone help me please. I want to consume restcountries with the above url and deserialize the data to use in my project. please help

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92

1 Answers1

-2

Try this!! JavaScriptSerializer json_serializer = new JavaScriptSerializer(); RootObject routes_list = (RootObject )json_serializer.DeserializeObject("{ \"test\":\"some data\"}");

  • I got this exception sir Unable to cast object of type 'System.Object[]' to type 'NSTestingHelper.Cjsondata.RootObject'. – Arun Developer Sep 09 '19 at 06:21
  • the Json might in the form of JArray. Make sure your class file matches the json structure using [json2c#](http://json2csharp.com) – Sudhir smart Sep 10 '19 at 14:05