问题
Ok So I have been asked to create a new question I have got my json now returning fine and into my class of city fine and dandy now using the following structure. But my quesiton is how do i return it as a List so that can be bound to a listview for example my get call is as follows
public async Task<City> GetCityListAsync()
{
var tcs = new TaskCompletionSource<City>();
string jsonresult = await WCFRESTServiceCall("GET", "cinema_city");
var list = await Task.Run(() => jsonresult.Deserialize<City>());
tcs.SetResult(list);
// for testing to show json being returned
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();
return await tcs.Task;
}
This is where I am calling my method
private async void citys_loaded(object sender, RoutedEventArgs e)
{
popcornpk_Dal popcorn_dal = new popcornpk_Dal();
City _mycitys = await popcorn_dal.GetCityListAsync();
var listView = (ListView)sender;
listView.ItemsSource = _mycitys.ToString();
}
So My main quesiton is how do i return a list of objects to my listview cause when i process i am just getting popcorn.dal.city in the xaml
XAML Layout
<Pivot x:Name="myPivot">
<PivotItem x:Name="pivot_item1" Header="by city" Margin="10,-76,28,-20.833">
<StackPanel Height="505">
<Grid Margin="0,0,0,118" Height="514" >
<ListView x:Name="listByCity" ItemsSource="{Binding}" Loaded="citys_loaded">
<DataTemplate>
<TextBlock x:Name="City" FontSize="14" Text="{Binding timing_title}"></TextBlock>
</DataTemplate>
</ListView>
</Grid>
</StackPanel>
</PivotItem>
Class for completness of quesiton
public class City
{
public string id { get; set; }
public string timing_title { get; set; }
}
public class Citys
{
public List<City> city { get; set; }
}
My Helper Function here
public static T Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<T>(SerializedJSONString);
return stuff;
}
Edit to show webserivce call which is php mysql based i dont need change that side just .net side into a list
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;
}
}
Also when calling a webservice what is the best method for error checking here I no i could use try catch and put a message say services could not be contacted but are their spefic messages I should be trapping for.
cinema_city method returns this json
{"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"}]}
回答1:
You are asking the same question again. If you have this json:
{"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"}]}
You need to deserialize it to your Citys class ( btw rename it to Cities maybe? )
So your code going to be:
public async Task<Citys> GetCityListAsync()
{
var tcs = new TaskCompletionSource<City>();
string jsonresult = await WCFRESTServiceCall("GET", "cinema_city");
var list = await Task.Run(() => jsonresult.Deserialize<Citys>());
tcs.SetResult(list);
// for testing to show json being returned
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();
return await tcs.Task;
}
or in case you want to return just List and not the wrapper:
public async Task<List<City>> GetCityListAsync()
{
var tcs = new TaskCompletionSource<List<City>>();
string jsonresult = await WCFRESTServiceCall("GET", "cinema_city");
var list = await Task.Run(() => jsonresult.Deserialize<Citys>());
tcs.SetResult(list.city);
// for testing to show json being returned
var dialog = new MessageDialog(jsonresult);
await dialog.ShowAsync();
return await tcs.Task;
}
Also I'm not sure why you wrote code this way. json.net async mehods are just wrappers around sync methods. so I'd simplify your code to:
public async Task<Citys> GetCityListAsync(){
var json = await WCFRESTServiceCall("GET", "cinema_city");
return json.Deserialize(Citys);
}
来源:https://stackoverflow.com/questions/31952133/return-list-instead-of-an-item-webservice-phone-8-1-uap