从Json获取数据并将其显示在Combobox中

时间:2019-05-22 04:40:44

标签: c# json winforms

我正在尝试使用url获取json文件并反序列化json并在组合框中显示特定字段,但没有显示

WebClient client = new WebClient();
string json = 
client.DownloadString("https://restcountries.eu/rest/v2/all/");
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer deserializer = new 
DataContractJsonSerializer(typeof(RootObject));
RootObject obj = (RootObject)deserializer.ReadObject(ms);
foreach (var name in obj.name)
{
comboBox1.Items.Add(obj.name);
}
}

这是POCO课

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

我想在组合框中显示名称

这是指向json的链接 https://restcountries.eu/rest/v2/all/

2 个答案:

答案 0 :(得分:1)

您尝试反序列化一个结果,但是结果是列表。

WebClient client = new WebClient();
string json = 
client.DownloadString("https://restcountries.eu/rest/v2/all/");
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer deserializer = new 
DataContractJsonSerializer(List<RootObject>); //TODO: FIXED
List<RootObject>obj = (List<RootObject>)deserializer.ReadObject(ms); //TODO: FIXED
foreach (var name in obj.name)
{
comboBox1.Items.Add(obj.name);
}
}

它的工作

答案 1 :(得分:0)

@ AM.Patel 我无法将其发布在评论部分 您发送的代码效果很好 我也用过newtonsoft.json 如果我不惹你 您能在代码中找出我的错误吗?

我尝试过一次输入的第一个代码

HttpClient http = new HttpClient();
string url = "https://restcountries.eu/rest/v2/alpha/ind";
HttpResponseMessage response = http.GetAsync(new Uri(url)).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
var countries = JsonConvert.DeserializeObject(responseBody);
var details = JObject.Parse(countries.ToString());
comboBox1.Items.Add(countrynames);

效果很好

我尝试输入多个条目的第二个代码在组合框中没有显示任何值,组合框保持为空

HttpClient http = new HttpClient();
IDictionary<String, Int32> countrycounts = new Dictionary<String, Int32>();
string url = "https://restcountries.eu/rest/v2/all/";
HttpResponseMessage response = http.GetAsync(new Uri(url)).Result;
string responseBody = response.Content.ReadAsStringAsync().Result;
var countries = JsonConvert.DeserializeObject(responseBody);
var details = JObject.Parse(countries.ToString());
foreach (var obj in details){
string countrynames = details["name"].ToString();
if (countrycounts.ContainsKey(countrynames))
{
int count = countrycounts[countrynames];
count++;
countrycounts[countrynames] = count;
comboBox1.Items.Add(countrynames);
}
else { comboBox1.Items.Add(countrynames); }