没有类对象如何反序列化呢?

时间:2019-09-09 05:42:15

标签: c# javascriptserializer

我有这个Restcountries网址https://restcountries.eu/rest/v2/all

我想用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());
}

反序列化的类:

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; }
}

当我运行时,没有反序列化的数据出现。仅返回null。有什么问题,谁能帮我。我想使用上述url使用restcountries并反序列化要在我的项目中使用的数据。请帮助

1 个答案:

答案 0 :(得分:-2)

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