I manged to get my json returned but I am having trouble returning it back into my class says cause its array. But I am using a deserialize method helper i studied from books. And should work.
So the question I need answered is how does my class need to be properly structured to decode the json into the city object.
I am grabbing my json via method which works
/// <summary>
/// Utility function to get/post WCFRESTService
/// </summary>
/// <param name="methodRequestType">RequestType:GET/POST</param>
/// <param name="methodName">WCFREST Method Name To GET/POST</param>
/// <param name="bodyParam">Parameter of POST Method (Need serialize to JSON before passed in)</param>
/// <returns>Created by David</returns>
private async Task<string> WCFRESTServiceCall(string methodRequestType, string methodName, string bodyParam = "")
{
string ServiceURI = "/launchwebservice/index.php/webservice/" + methodName;
HttpClient httpClient = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(methodRequestType == "GET" ? HttpMethod.Get : HttpMethod.Post, ServiceURI);
if (!string.IsNullOrEmpty(bodyParam))
{
request.Content = new StringContent(bodyParam, Encoding.UTF8, "application/json");
}
HttpResponseMessage response = await httpClient.SendAsync(request);
string jsongString = await response.Content.ReadAsStringAsync();
return jsongString;
}
Then I am using the deserialize method
public static class Helpers
{
public static List<T> Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<List<T>>(SerializedJSONString);
return stuff;
}
}
I am calling the above in the following manner
string jsonresult = await WCFRESTServiceCall("GET", "cinema_city");
var data = jsonresult.Deserialize<Citys>();
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();
Which when I check jsonresult
it is indeed returning the json but in square brackets for some reason.
Which gives me the following error:
My Class of city is as follows
public class City
{
public string id { get; set; }
public string timing_title { get; set; }
}
public class Citys
{
public List<City> city { get; set; }
}
Edit To Show The jason data
{"city":[{"id":"5521","timing_title":"Lahore"},{"id":"5517","timing_title":"Karachi"},{"id":"5538","timing_title":"Islamabad"},{"id":"5535","timing_title":"Rawalpindi"},{"id":"5518","timing_title":"Hyderabad"},{"id":"5512","timing_title":"Faisalabad"},{"id":"8028","timing_title":"Gujranwala"},{"id":"8027","timing_title":"Gujrat"}]}
Edit to show error
Error on dezerilize event ?
For your classes json going to be:
For City class
{ id: "some string", timing_title:"some string" }
and for Citys class:
{ city: [ { id: "some string", timing_title:"some string" }, ... ] }
I belive you have a typo in property name - it is city now, but really should be cities.
Another issue I see is that you are mixing together Citys class and List. They are not the same and you cannot serialize/deserialize them to each other.
Correct json for List going to be:
[ { id: "some string", timing_title:"some string" } ,... ]
compare it to Citys class above.
UPDATE: After reading it again, you are trying to deserialize List to Citys. That is what your exeption says. Probably that is what you wanted to say:
public static class Helpers
{
public static T Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<T>(SerializedJSONString);
return stuff;
}
}
string jsonresult = await WCFRESTServiceCall("GET", "cinema_city");
var data = jsonresult.Deserialize<Citys>();
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();