在json上从json获取数据 [英] get data from json on json

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

问题描述

问题:

如何从下面 json 的bundle_file"属性(我放红框的数据)中检索数据?

How do I retrieve data from "bundle_file" property (data that I put a red box) of json below?

我有从上面的 JSON 检索数据的代码,但我对从捆绑文件"属性检索数据感到困惑.所以我需要获取所有数据,包括bundle_file"上的数据

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"

代码:

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;
            }

        }

推荐答案

您可以为 JSON 创建 DataContract 属性类并使用 DataContractJsonSerializer 对数据进行反序列化.之后只需执行 LINQ 查询即可获取所需的数据.如果 JSON 数据发生变化,这将高效且轻松地处理任何进一步的变化.

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.

下面是您可以用来解析 JSON 的类(我使用了 https://jsonutils.com/生成类)

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; }
}

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

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