gpt4 book ai didi

c# - 返回列表而不是项目 Webservice Phone 8.1 UAP

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:39 25 4
gpt4 key购买 nike

好的 所以我被要求创建一个新问题 我现在使用以下结构让我的 json 正常返回并进入我的 city fine and dandy 类。但我的问题是如何将它作为列表返回,以便可以绑定(bind)到 ListView ,例如我的 get 调用如下

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

这是我调用方法的地方

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

所以我的主要问题是如何将对象列表返回到我的 ListView ,因为当我处理时我只是在 xaml 中获取 popcorn.dal.city

XAML 布局

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

问题完整性等级

    public class City
{
public string id { get; set; }
public string timing_title { get; set; }
}
public class Citys
{
public List<City> city { get; set; }
}

这里是我的辅助函数

public static T Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<T>(SerializedJSONString);
return stuff;
}

编辑以显示基于 php mysql 的 webserivce 调用,我不需要将 .net 端更改为列表

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

此外,在调用 Web 服务时,这里进行错误检查的最佳方法是什么?我不可以使用 try catch 并发送一条消息说无法联系服务,但我应该捕获它们的特定消息。

cinema_city 方法返回这个 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"}]}

最佳答案

你又问了同样的问题。如果你有这个 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"}]}

您需要将其反序列化为您的 Citys 类(顺便说一句,也许将其重命名为 Cities?)

所以你的代码将是:

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

或者如果您只想返回列表而不是包装器:

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

另外我不确定你为什么要这样写代码。 json.net 异步方法只是同步方法的包装器。所以我会将您的代码简化为:

 public async Task<Citys> GetCityListAsync(){ 
var json = await WCFRESTServiceCall("GET", "cinema_city");
return json.Deserialize(Citys);
}

关于c# - 返回列表而不是项目 Webservice Phone 8.1 UAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31952133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com