如何从uri中解析Xamarin Android上的列表



我尝试了太多,但没有成功

这是我从Web服务中获取JSON String并将其列出的方法,我想在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);
        }
    }

当我调用下载dataAsync((;我有一个错误:

发生了一个未经治疗的例外。

什么是解决方案?

我的Web服务方法中有参数,我可以处理谁?

这是我的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; }
}

请尝试:

 public async void DownloadDataAsync()
    {
        try
        {
            string url = "http://myWebSite.com/jWebService.asmx/GetOffersJSON?storeID=2";
            var httpClient = new HttpClient();
            var content = await httpClient.GetStringAsync(url);
            // de-serializing json response into list, with filtering before
            var startPosition = content.IndexOf('>') + 1;
            var endPosition = content.LastIndexOf("</", StringComparison.Ordinal);
            var filteredResponseCharArray = new char[endPosition - startPosition];
            content.CopyTo(startPosition, filteredResponseCharArray, 0, endPosition - startPosition);
            var listOfOffers = JsonConvert.DeserializeObject<List<offers>>(new string(filteredResponseCharArray));
        }
        catch (Exception error)
        {
            Debug.WriteLine(error);
            throw;
        }
    }

您应该更改网络服务以获得无XML结构的有效JSON响应。

相关内容

  • 没有找到相关文章

最新更新