错误在gridview ItemClick上显示json数据

时间:2016-06-09 02:22:27

标签: c# arrays uwp getjson

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

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并且数据不为空,但是如果它被放在数据上则变为null itemDetail.BundleName

如何处理?

2 个答案:

答案 0 :(得分:0)

这意味着itemDetail.BundleName null您可以通过以下代码解决此问题:

DetailBundleName.Text = itemDetail.BundleName==null?"":itemDetail.BundleName;

哪个会检查BundleName是否为空,如果为空,则为""分配DetailBundleName.Text,否则它会分配BundleName的值。

注意: - 如果在每个属性中获取null的更改,那么每次从中访问值时都要检查每个属性。而不是那样处理它在Property getter中;以下代码将为您提供帮助:

class BukuAudio
{
    private string _BundleName;

    public string MyProperty
    {
        get
        {
            if (_BundleName == null) return "";

            return _BundleName;
        }
        set { _BundleName = value; }
    }
}

答案 1 :(得分:0)

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

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

但是你错过了设置文件对象的BundleName。 要解决此问题,请修改以下代码:

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.

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