【发布时间】:2017-04-19 20:17:04
【问题描述】:
我尝试了太多但没有成功
这是我从 Web 服务 Uri 获取 JSON 字符串并将其反序列化为列表的方法,我想在 Xamarin Android App 上使用它
public async void DownloadDataAsync()
{
string url = "http://myWebSite.com/jWebService.asmx/GetOffersJSON?storeID=2";
var httpClient = new HttpClient();
Task <string> downloadTask = httpClient.GetStringAsync(url);
string content = await downloadTask;
// de-serializing json response into list
JObject jsonResponse = JObject.Parse(content);
IList<JToken> results = jsonResponse["offs"].ToList();
foreach (JToken token in results)
{
offers poi = JsonConvert.DeserializeObject<offers>(token.ToString());
offs.Add(poi);
}
}
当我调用 DownloadDataAsync();我收到一个错误:
发生未处理的异常。
解决办法是什么?
我的网络服务方法上有参数,我可以处理它吗?
这是我的 JSON Uri 结果:
This XML file does not appear to have any style information associated with
it. The document tree is shown below.
<string xmlns="http://tempuri.org/">[{"ItemID":20,"ItemBarcode":"111","ItemName":"hgh","ItemImage":"MegaOrders22017-04-14-08-34-27.jpg","ItemPrice":7.0000,"ItemNotes":"gffgdfj","OfferOn":true},{"ItemID":21,"ItemBarcode":"222","ItemName":"Nod","ItemImage":"MegaOrders22017-04-14-08-34-57.jpg","ItemPrice":4.0000,"ItemNotes":"kkkkkk","OfferOn":true},{"ItemID":22,"ItemBarcode":"333","ItemName":"kjkjkjkj","ItemImage":"MegaOrders22017-04-14-08-35-21.jpg","ItemPrice":6.0000,"ItemNotes":"hhhhggggg","OfferOn":true},{"ItemID":23,"ItemBarcode":"4444","ItemName":"oioioio","ItemImage":"MegaOrders22017-04-14-08-35-50.jpg","ItemPrice":5.0000,"ItemNotes":"hjhgfdfghj","OfferOn":true}]
</string>
我使用的类:
public class offers
{
public int ItemID { get; set; }
public string ItemBarcode { get; set; }
public string ItemName { get; set; }
public string ItemImage { get; set; }
public double ItemPrice { get; set; }
public string ItemNotes { get; set; }
public bool OfferOn { get; set; }
}
【问题讨论】:
-
错误应该有更多细节,比如你的方法失败的堆栈跟踪。
-
不幸的是,我只出现了这个错误文本,你能告诉我代码是否正确吗?
标签: c# json web-services xamarin.android