在gridview ItemClick上显示json数据时出错 [英] Error display json data on gridview ItemClick

查看:79
本文介绍了在gridview ItemClick上显示json数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过从JSON(数组中的数组)获取数据来显示数据时,我遇到了问题.即在选择gridview时,然后立即退出应用程序,并显示错误消息,如下所示: 代码:

I have problems when displaying data by taking data from JSON (arrays in the array). That is when the gridview selected, then immediately exit the application and will display an error message, as below: Code:

private BukuAudio itemDetail = null;

public async void StoreAll()
{
     try
    {
        var client = new Windows.Web.Http.HttpClient();
        string urlPath = "website";
        var values = new List<KeyValuePair<string, string>>
        {

        };
        var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values));
        response.EnsureSuccessStatusCode();
        string jsonText = await response.Content.ReadAsStringAsync();
        JsonObject jsonObject = JsonObject.Parse(jsonText);
        JsonArray jsonData1 = jsonObject["data"].GetArray();

        foreach (JsonValue groupValue in jsonData1)
        {
                JsonObject groupObject = groupValue.GetObject();
                string nid = groupObject["sku"].GetString();
                string title = groupObject["judul"].GetString();
                string deskripsi = groupObject["deskripsi"].GetString();
                string tipe = groupObject["tipe"].GetString();
                var bundleObj = groupObject["bundle"];
                if (bundleObj.ValueType == JsonValueType.Array)
                {
                    JsonArray bundle = bundleObj.GetArray();
                    foreach (JsonValue groupValue1 in bundle)
                {

                    JsonObject groupObject1 = groupValue1.GetObject();
                        string bundleName = groupObject1["bundle_file"].GetString();
                        string pathFile = groupObject1["path_file"].GetString();
                        BukuAudio file1 = new BukuAudio();
                    file1.BundleName = bundleName;
                                file1.Tipe = tipe1;
                                if (file1.Tipe == "0")
                                {
                                    file1.BundlePath = pathFile + bundleName + ".pdf";
                                }
                                else if (file1.Tipe == "1")
                                {
                                    file1.BundlePath = pathFile + bundleName + ".mp3";
                                }
                }
            }

            BukuAudio file = new BukuAudio();
            file.SKU = nid;
            file.Judul = title;
            file.Deskripsi = deskripsi;
            file.Tipe = tipe;

            if (bundleObj.ValueType == JsonValueType.Array)
            {
                datasource.Add(file);
           }
        }

        if (jsonData1.Count > 0)
        {
            itemGridView.ItemsSource = datasource;
        }
    }
    catch
    {
    }

private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
    ProductDetail.IsOpen = true;
    itemDetail = e.ClickedItem as BukuAudio;
    DetailSKU.Text = itemDetail.SKU;
    DetailJudul.Text = itemDetail.Judul;
    DetailDeskripsi.Text = itemDetail.Deskripsi;
    DetailBundleName.Text = itemDetail.BundleName;
    DetailTipe.Text = itemDetail.Tipe;
}

我已经调试了file1.bundleName并且数据不为空,但是如果将其放在数据上,则该数据将变为空itemDetail.BundleName

I've debug file1.bundleName and data is not empty, but if it is put on the data becomes null itemDetail.BundleName

如何处理?

推荐答案

从您的代码中,添加到gridview的项目是文件对象:

From your codes, the items that added to your gridview is the file object:

if (bundleObj.ValueType == JsonValueType.Array)
{
     datasource.Add(file);
}

但是您错过了设置文件对象的BundleName的权限. 要解决此问题,请修改您的代码,如下所示:

But you miss setting the BundleName of file object. To fix the problem revise your codes like below:

foreach (JsonValue groupValue in jsonData1)
    {
            ...
            string tipe = groupObject["tipe"].GetString();
            string bundleName="";//This line should be added;
            var bundleObj = groupObject["bundle"];
            if (bundleObj.ValueType == JsonValueType.Array)
            {
                JsonArray bundle = bundleObj.GetArray();
                foreach (JsonValue groupValue1 in bundle)
            {

                JsonObject groupObject1 = groupValue1.GetObject();
                bundleName = groupObject1["bundle_file"].GetString();//This line should be edited;
                    ...
            }
        }

        BukuAudio file = new BukuAudio();
        file.SKU = nid;
        file.Judul = title;
        file.Deskripsi = deskripsi;
        file.Tipe = tipe;
        file.BundleName=bundleName;//This line should be added.

注意:我在修改代码的行中添加了注释.

Notes:I added comments in which line I revised the codes.

这篇关于在gridview ItemClick上显示json数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆