问题
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;
}
I've debug file1.bundleName and data is not empty, but if it is put on the data becomes null itemDetail.BundleName
How to handle it?
回答1:
Which means that itemDetail.BundleName
is null
you can solve this issue by the following code:
DetailBundleName.Text = itemDetail.BundleName==null?"":itemDetail.BundleName;
Which will check whether BundleName
is null or not, if it is null the assign ""
to the DetailBundleName.Text
else it assign the value of the BundleName
.
Note :- If there is a change for getting null in each property then its a wate effort to check every property every time when you access the value from it. Instead of doing that Handle it in the Property getter; The following code will help you:
class BukuAudio
{
private string _BundleName;
public string MyProperty
{
get
{
if (_BundleName == null) return "";
return _BundleName;
}
set { _BundleName = value; }
}
}
回答2:
From your codes, the items that added to your gridview is the file object:
if (bundleObj.ValueType == JsonValueType.Array)
{
datasource.Add(file);
}
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.
来源:https://stackoverflow.com/questions/37715845/error-display-json-data-on-gridview-itemclick