如何访问这个深层嵌套的JSON的变量

时间:2017-09-09 04:51:46

标签: c# json unity3d

我收到一个JSON以响应请求。我能够访问简单的变量并在屏幕上打印它们。但我不知道如何访问深度变量并在屏幕上打印它。我想访问里面的变量“提供“课程。我怎么去做? 这是我的模特课。

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class Fields
{
}
[Serializable]
public class Queries
{
}
[Serializable]
public class FacetResults
{
    public Fields fields;
    public Queries queries;
}
[Serializable]
public class Fields2
{
}
[Serializable]
public class Facet
{
    public Fields2 fields;
    public List<object> queries;
}
[Serializable]
public class Fields3
{
}
[Serializable]
public class Filter
{
    public Fields3 fields;
    public List<object> queries;
}
[Serializable]
public class Stats
{
}
[Serializable]
public class Request
{
    public string __class__;
    public Facet facet;
    public Filter filter;
    public int limit;
    public string q;
    public string sortby;
    public int start;
    public Stats stats;
}
[Serializable]
public class Brand
{
    public string __class__;
    public string homepage_url ;
    public string name ;
    public string uid ;
}
[Serializable]
public class Manufacturer
{
    public string __class__ ;
    public string homepage_url ;
    public string name ;
    public string uid ;
}
[Serializable]
public class Prices
{
    public List<List<object>> USD ;
    public List<List<object>> GBP ;
    public List<List<object>> INR ;
    public List<List<object>> CNY ;
    public List<List<object>> EUR ;
    public List<List<object>> JPY ;
}
[Serializable]
public class Seller
{
    public string __class__;
    public string display_flag ;
    public bool has_ecommerce;
    public string homepage_url;
    public string id ;
    public string name ;
    public string uid ;
}
[Serializable]
public class Offer
{
    public string __class__ ;
    public string _naive_id ;
    public string eligible_region;
    public int? factory_lead_days;
    public int? factory_order_multiple;
    public int in_stock_quantity ;
    public bool is_authorized ;
    public bool is_realtime ;
    public string last_updated;
    public int moq ;
    public object octopart_rfq_url;
    public string on_order_eta ;
    public int? on_order_quantity ;
    public int? order_multiple ;
    public string packaging ;
    public Prices prices ;
    public string product_url ;
    public Seller seller ;
    public string sku;
}
[Serializable]
public class Item
{
    public string __class__ ;
    public Brand brand ;
    public Manufacturer manufacturer ;
    public string mpn ;
    public string octopart_url ;
    public List<Offer> offers ;
    public List<string> redirected_uids ;
    public string uid ;
}
[Serializable]
public class Result
{
    public string __class__ ;
    public Item item ;
    public string snippet ;
}
[Serializable]
public class SpecMetadata
{
}
[Serializable]
public class StatsResults
{
}
[Serializable]
public class RootOb
{
    public string __class__ ;
    public FacetResults facet_results;
    public int hits ;
    public int msec ;
    public Request request; 
    public List<Result> results; 
    public SpecMetadata spec_metadata; 
    public StatsResults stats_results;
    public string user_country ;
    public string user_currency ;
}

以下是我想要访问的课程。

   namespace OctopartApi
{
    using Newtonsoft.Json;
    using RestSharp;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine;
    using System.Net;
using UnityEngine.UI;
public  class KeywordSearch1 : MonoBehaviour
{
    public InputField mainInputField;
    public Canvas can;
    public Text parts,package,pin,dersc;
    public float x, y;
    void Start () {
        mainInputField.onEndEdit.AddListener(delegate {LockInput(mainInputField); });

    }
    void LockInput(InputField input)
    {
        ExecuteSearch (input.text);

    }
    public  void ExecuteSearch(string inp)
    {
        // -- your search query --
        string query = inp;

        string octopartUrlBase = "http://octopart.com/api/v3";
        string octopartUrlEndpoint = "parts/search";
        string apiKey = "57af648b";

        // Create the search request

        var client = new RestClient(octopartUrlBase);
        var req = new RestRequest(octopartUrlEndpoint, Method.GET)
                    .AddParameter("apikey", apiKey)
                    .AddParameter("q", query)
                    .AddParameter("start", "0")
                    .AddParameter("limit", "10");
        var resp = client.Execute(req);
        string octojson = resp.Content;
        RootOb rr = JsonUtility.FromJson<RootOb> (octojson);
        Debug.Log (rr.hits);
        printingPress (rr.results);
        parts.horizontalOverflow = HorizontalWrapMode.Wrap;
        parts.verticalOverflow = VerticalWrapMode.Overflow;
    }
    public void printingPress( List<Result> reslt)
    {
        for (var i = 0; i < reslt.Count; i++) {
            parts.text = parts.text+reslt [i].item.brand.name+"\n";
        }
    }
    private const string APIKEY = "57af648b";
 }
}

我的Json太大了,无法在此发布,所以这里有一个链接:https://octopart.com/api/docs/v3/search-tutorial

1 个答案:

答案 0 :(得分:0)

只是为了将来参考,您可以通过查看首先声明Offer的位置然后从那里下载来找到它。

1 Offer类在 Item 类中声明为public List<Offer> offers;

2 Item类在 Result 类中声明为public Item item;

3 。最后,Result类在 RootOb 类中声明为公共List<Result> results;

找到后,您可以通过反转来访问Offer课程。从#3 开始,回到#1

 RootOb rr = JsonUtility.FromJson<RootOb>(octojson);

<强>#3 即可。List<Result> result = rr.results;

<强>#2 即可。 Item item = result[0].item;

可以有多个Result,因为它是List。在访问之前,您可以使用result.Count检查Item的数量。{/ p>

Item item1 = result[0].item;

Item item2 = result[1].item;

Item item3 = result[2].item;

<强>#1 即可。Offer offer = item.offers[0];

可以有多个Offer,因为它是List。您可以使用item.offers.Count检查访问前的数量。

Offer offer1 = item.offers[0];

Offer offer2 = item.offers[1];

Offer offer3 = item.offers[2];

同样,就是这样:

RootOb rr = JsonUtility.FromJson<RootOb>(octojson);
List<Result> result = rr.results;
Item item = result[0].item;
Offer offer = item.offers[0];

最后,最好用for循环完成:

RootOb rr = JsonUtility.FromJson<RootOb>(octojson);
List<Result> result = rr.results;
//Loop through the Results List
for (int i = 0; i < result.Count; i++)
{
    //Get each item
    Item item = result[i].item;
    Debug.Log("Item Index: " + i + "...  mpn: " + item.mpn);

    //Loop through the Offer List
    for (int j = 0; j < item.offers.Count; j++)
    {
        //Get each offer
        Offer offer = item.offers[j];
        Debug.Log("Offer Index: " + j + "....  quantity: " + offer.in_stock_quantity);
    }
}