【发布时间】:2019-09-09 05:42:15
【问题描述】:
我有这个restcountries url 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 并反序列化要在我的项目中使用的数据。请帮忙
【问题讨论】:
-
朋友们好,我复制了restcountries json并使用json2csharp.com/#创建了类对象,我无法使用数据。请帮忙。
-
为什么在使用 JavaScriptSerializer 时标记了 Json.NET?
-
自我提醒:最近所有的连载问题是怎么回事?
-
@MickyD 也许是满月。