我收到一個JSON以響應請求。我可以訪問簡單變量並在屏幕上打印它們。但我不確定如何訪問深層變量並將其打印出來我想要訪問「Offers」類中的變量。我該如何去解決它? 這是我的模特班。如何訪問此深度嵌套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是太大所以張貼在這裏,這裏是一個鏈接:通過查看哪裏Offer
首先宣告然後再從下https://octopart.com/api/docs/v3/search-tutorial
好的。我欠你 :)。非常感謝。 。我現在會試試 –
YE,它它'result.count'。我在內循環中犯了一個錯誤。這應該是'item.offers.Count'。在最後一個循環示例中查看我的編輯。爲該循環代碼添加了更多評論。 – Programmer
耶現在完美:) –