Reputation: 5189
So I have a City class that I want to be able to get and store JSON data.
public string status { get; set; }
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; }
}
In my App I need to request a City by an ID or display a list of Cities by a country code so I want the City to be a base class.
Here is the code I am trying to use to make this happe. It is in a WebRetrieval class
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;
}
//Return City
async public Task<City> GetCity(string url)
{
City city = await GetCityInformation(url);
return city;
}
And then when I try to make a City object in the City_Page class, I use this:
long cityNum = Convert.ToInt64(CityID_Textbox.Text);
string url = "http://honey.computing.dcu.ie/city/city.php?id=" + cityNum;
City city = wr.GetCity(url);
myTextBlock1.Text = city.ToString();
I keep getting erros about System.Threading.tasks.task not being able to be converted to CityApp.City.
I'm really stumped and I don't know what to do. Right now the code is broken and I have no idea how to fix it. So I would really appreciate any help with it. Thanks
Upvotes: 1
Views: 354
Reputation: 4104
Change
City city = wr.GetCity(url);
to
City city = wr.GetCity(url).Result;
if you're using .Net 4.0; if you're using 4.5, then
City city = await wr.GetCity(url);
Also, you're returning a CityResponse not just a City; the Json is failing to deserialize because it lacks [] to define the array. If you do the following
var cityResponse = new CityResponse();
cityResponse.status = "okay";
cityResponse.result = new List<City>();
var city = new City();
city.Status = "0";
city.Id = "1";
city.Country = "2";
city.Region = "3";
city.MainCity = "4";
city.Latitude = "5";
city.Longitude = "6";
city.Comment = "7";
city.WasThereAnError = false;
cityResponse.result.Add(city);
var jsonString = JsonConvert.SerializeObject(cityResponse);
jsonString will be
"{\"status\":\"okay\",\"Message\":null,\"result\":[{\"Status\":\"0\",\"Id\":\"1\",\"Country\":\"2\",\"Region\":\"3\",\"MainCity\":\"4\",\"Latitude\":\"5\",\"Longitude\":\"6\",\"Comment\":\"7\",\"WasThereAnError\":false}]}"
Upvotes: 2
Reputation: 68740
To add to the other answers, you should delete your SerializeObject
statement and simply deserialize the string into a City
object.
//result = JsonConvert.SerializeObject(response);
var city = JsonConvert.DeserializeObject<City>(result);
Upvotes: 1
Reputation: 432
GetCity already awaits and should retung City instead of Task.
Upvotes: 1
Reputation: 26873
Your GetCity
method is async
, so you have to await
it to get a City
instead of a Task<City>
.
City city = await wr.GetCity(url);
Upvotes: 1