取JSON中数组中的数据数组
Taking the data array in the array in JSON
如何在下面的 json 中检索 "bundle file" 数据(在下图中我给出红色圆圈的部分)?
我试试:
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 tanggal = groupObject["tgl"].GetString();
string tipe = groupObject["tipe"].GetString();
string namaTipe = groupObject["nama_tipe"].GetString();
string gratis = groupObject["gratis"].GetString();
string dataFile = groupObject["nfile"].GetString();
string harga = groupObject["hrg"].GetString();
JsonArray bundle = groupObject["bundle"].GetArray();
foreach(JsonValue groupValue1 in bundle)
{
JsonObject groupObject1 = groupValue1.GetObject();
string bundleName = groupObject1["bundle_file"].GetString();
string pathFile = groupObject1["path_file"].GetString();
}
}
但是在代码JsonArray bundle = groupObject["bundle"].GetArray();
中,显示错误信息如下:
而 "bundle_file" 类型是数组。
如何解决?
This is not an Array Value. Use ValueType property to get the type.
从报错信息中我们可以知道这是一个类型错误。我下载了Json文件,发现问题出在数据上,这个Json文件有部分"bundle"是字符串类型:
所以当你打电话给groupObject["bundle"].GetArray()
。出错了。
要解决此问题,只需使用如下 if 语句包装部分代码:
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();
}
}
错误将消失。
如何在下面的 json 中检索 "bundle file" 数据(在下图中我给出红色圆圈的部分)?
我试试:
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 tanggal = groupObject["tgl"].GetString();
string tipe = groupObject["tipe"].GetString();
string namaTipe = groupObject["nama_tipe"].GetString();
string gratis = groupObject["gratis"].GetString();
string dataFile = groupObject["nfile"].GetString();
string harga = groupObject["hrg"].GetString();
JsonArray bundle = groupObject["bundle"].GetArray();
foreach(JsonValue groupValue1 in bundle)
{
JsonObject groupObject1 = groupValue1.GetObject();
string bundleName = groupObject1["bundle_file"].GetString();
string pathFile = groupObject1["path_file"].GetString();
}
}
但是在代码JsonArray bundle = groupObject["bundle"].GetArray();
中,显示错误信息如下:
而 "bundle_file" 类型是数组。
如何解决?
This is not an Array Value. Use ValueType property to get the type.
从报错信息中我们可以知道这是一个类型错误。我下载了Json文件,发现问题出在数据上,这个Json文件有部分"bundle"是字符串类型:
所以当你打电话给groupObject["bundle"].GetArray()
。出错了。
要解决此问题,只需使用如下 if 语句包装部分代码:
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();
}
}
错误将消失。