问题
Problem :
How do I retrieve data from "bundle_file" property (data that I put a red box) of json below?
I have the code to retrieve data from JSON above, but I was confused to retrieve data from a "bundle file" property. So I need to take all the data, including data on "bundle_file"
Code:
try
{
var client = new Windows.Web.Http.HttpClient();
string urlPath = "http://mhndt.com/newsstand/renungan-harian/callback/allWinItems";
var values = new List<KeyValuePair<string, string>>
{
//new KeyValuePair<string, string>("hal", "1"),
//new KeyValuePair<string, string>("limit","300")
};
var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();
if (!response.IsSuccessStatusCode)
{
RequestException();
}
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();
//List<object> list = jsonData1.ToList<object>();
//JsonObject jsonData2 = jsonObject["data"].GetObject();
//JsonArray jsonDataBundle = list["bundle_file"].GetArray();
//foreach(JsonValue groupValue1 in jsonDataBundle)
//{
// JsonObject groupObject1 = groupValue1.GetObject();
// string bundleName = groupObject["bundle_file"].GetString();
// string pathFile = groupObject["path_file"].GetString();
//}
BukuAudio file = new BukuAudio();
file.SKU = nid;
file.Judul = title;
file.Deskripsi = deskripsi;
string[] formats = { "d MMMM yyyy" };
var dateTime = DateTime.ParseExact(tanggal, formats, new CultureInfo("id-ID"), DateTimeStyles.None);
Int64 n = Int64.Parse(dateTime.ToString("yyyyMMdd"));
file.Tanggal = n.ToString();
int tgl = Int32.Parse(file.Tanggal);
file.Tipe = tipe;
file.NamaTipe = "Tipe: " + namaTipe;
file.Gratis = gratis;
file.File = "http://mhndt.com/newsstand/rh/item/" + dataFile;
file.Cover = "http://mhndt.com/newsstand/rh/item/" + dataFile + ".jpg";
if (licenseInformation.ProductLicenses[file.SKU].IsActive)
{
file.Harga = "Purchased";
}
else
{
if (file.Gratis == "1")
{
file.Harga = "Free";
}
else
{
file.Harga = harga;
}
}
if (tgl >= 20150201 || file.Judul == "RH Anak Volume 01 : Yesus Sahabatku")
{
datasource.Add(file);
}
}
if (jsonData1.Count > 0)
{
itemGridView.ItemsSource = datasource;
}
else
{
MessageDialog messageDialog;
messageDialog = new MessageDialog("Data kosong", "Buku atau Audio Tidak tersedia");
messageDialog.Commands.Add(new UICommand("Tutup", (command) =>
{
this.Frame.Navigate(typeof(MainPage));
}));
await messageDialog.ShowAsync();
}
}
catch (HttpRequestException ex)
{
RequestException();
busyindicator.IsActive = false;
}
}
回答1:
Try this it will work :
use for in loop to iterate the nested object data. I created the sample JSON as same you posted in the question for the reference.
JSON :
http://mhndt.com/newsstand/renungan-harian/callback/allWinItems
Iteration of the nested records :
for (var item in obj.data) {
if(typeof obj.data[item].bundle_file == 'object') {
for(var finalData in obj.data[item].bundle_file) {
console.log(obj.data[item].bundle_file[finalData].bundle_file);
console.log(obj.data[item].bundle_file[finalData].path_file);
console.log(obj.data[item].bundle_file[finalData].pwd_file);
}
}
}
Working fiddle : https://jsfiddle.net/rohitjindal/dzdpg8dw/3/
回答2:
You can create DataContract attributed class for the JSON and use DataContractJsonSerializer to deserialize the data. After that just perform a LINQ query to get the desired data. This will efficient and easy to handle any further changes if the JSON data changes.
Below is the class you can use to parse JSON (I used https://jsonutils.com/ to generate the class)
[DataContract]
public class Root
{
[DataMember]
public int total { get; set; }
[DataMember]
public int start { get; set; }
[DataMember]
public int next { get; set; }
[DataMember]
public IList<Datum> data { get; set; }
}
[DataContract]
public class Datum
{
[DataMember]
public string idfile { get; set; }
[DataMember]
public string judul { get; set; }
[DataMember]
public string sku { get; set; }
[DataMember]
public string tipe { get; set; }
[DataMember]
public string nama_tipe { get; set; }
[DataMember]
public string gratis { get; set; }
[DataMember]
public string hrg { get; set; }
[DataMember]
public string katid { get; set; }
[DataMember]
public string nfile { get; set; }
[DataMember]
public BundleFile bundle_file { get; set; }
[DataMember]
public string tgl { get; set; }
[DataMember]
public string ufile { get; set; }
[DataMember]
public string deskripsi { get; set; }
[DataMember]
public string isLokal { get; set; }
}
[DataContract]
public class BundleFile
{
[DataMember]
public string bundle_file { get; set; }
[DataMember]
public string path_file { get; set; }
[DataMember]
public string pwd_file { get; set; }
}
来源:https://stackoverflow.com/questions/37651350/get-data-from-json-on-json