-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

9
  • hello friends, I copy the restcountries json and create class object using json2csharp.com/#, I was not able to consume the data. please help. Commented Sep 9, 2019 at 5:43
  • 2
    Why have you tagged Json.NET when you're using JavaScriptSerializer? Commented Sep 9, 2019 at 5:43
  • 3
    app.quicktype.io?share=WYgQayheKxDr3XhrHnci might get you started. Commented Sep 9, 2019 at 5:44
  • 1
    Note to self: what's with all the serialisation questions recently? Commented Sep 9, 2019 at 5:50
  • @MickyD Maybe it's a full moon. Commented Sep 9, 2019 at 6:00

1 Answer 1

-2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I got this exception sir Unable to cast object of type 'System.Object[]' to type 'NSTestingHelper.Cjsondata.RootObject'.
the Json might in the form of JArray. Make sure your class file matches the json structure using json2c#

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.