因此,当我从服务器获取 JSON 数据时,我想构建一个 City 类。
public class City
{
public string id { get; set; }
public string country { get; set; }
public string region { get; set; }
public string mainCity { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string comment { get; set; }
public bool wasThereAnError { get; set; }
public class CityResponse
{
public string status { get; set; }
public string message { get; set; }
public List<City> result { get; set; }
}
public override string ToString()
{
return "\n\nID: \t\t" + id + "\nCountry: \t" + country + "\nRegion: \t\t" + region + "\nCity: \t\t" + mainCity + "\nLatitude: \t" + latitude + "\nLongitude: \t" + longitude + "\nComment: \t" + comment;
}
}
那是那里的课。所以当我查询例如:
{"status":"okay","result":{"id":1,"country":"US","region":"NY","city":"Valhalla","latitude":41.0877,"longitude":-73.7768,"comment":"890068 monkeys"}}
我希望 City 类填充相应的数据。这是我获取数据的网络检索类。
async private Task<City> GetCityInformation(string url)
{
var client = new HttpClient();
var response = await client.GetAsync(new Uri(url));
string result = await response.Content.ReadAsStringAsync();
//var cityRootaaa = JsonConvert.DeserializeObject<City.CityResponse>(result);
var cityRoot = JsonConvert.DeserializeObject<City>(result);
return cityRoot;
}
}
但是,当我调试时,我可以看到没有任何东西被保存。最初,我在 City 的 id 上方有另一个状态字段,当我调试时,状态字段中设置了“好的”,但没有其他数据。我不知道该怎么办?谢谢你的帮助!