2017-09-07 111 views
0

我使用unity3d,我有一個JSON對象。我可以使用ob.name等訪問每個成員,但我希望在運行時將這個反序列化的塊打印在屏幕上。類似於搜索結果,因此我得到了JSON作爲搜索結果我想在屏幕上顯示它。我得到的錯誤是我無法打印對象,因爲我使用了(ob.name)toString(); 我不確定如何在運行時在屏幕上顯示此信息。打印JSON到屏幕作爲一個團塊中的文本塊

ObjR rs = JsonUtility.FromJson<ObjR>(jsonString); 
//so now I want to print to screen each element of rs.How do I do that during runtime. 

編輯:我能看到的debug.log,我只需要動態上打印他們screen.Please說明,大小或結果的數量是在運行時,將vary.Any幫助表示讚賞。所以我找到了這個,現在我在屏幕上獲得它。 http://wiki.unity3d.com/index.php/DebugConsole.Can任何人都可以幫助我如何使每個響應點擊事件?

var client = new RestClient(Url); 
var req = new RestRequest(UrlEndpoint, Method.GET) 
      .AddParameter("apikey", apiKey) 
      .AddParameter("q", query) 

    // Perform the search and obtain results 
var resp = client.Execute(req); 
var search = JsonConvert.DeserializeObject<dynamic>(resp.Content); 

// Print the number of results 
Console.WriteLine("Number of hits: " + search["hits"]); 
Debug.Log(search["hits"] + " "); 
foreach (var result in search["results"]) 
{ 
    var part = result["item"]; 
    // want to print to screen each item and mpn 
    //Debug.Log(part["brand"]["name"] + " " + part["mpn"]); 
    //what i tried/ string hits = search["hits"].ToString();//error 

    //expected type object and found string 
    GUILabel(float,float,needed string here); 
} 
+0

你從哪裏得到這個錯誤嗎?請張貼您的其他代碼,並告訴我們錯誤在哪裏。 – Programmer

+0

@Programmer我試圖在運行時創建一個GUIText,但它需要傳遞一個字符串。所以我想將search [「hits」]轉換爲一個字符串。但是出現一個錯誤。所以我的目標是打印我所有的結果在屏幕上。 –

+0

您需要使用JsonUtility。請張貼你的json的樣子,我會告訴你如何打印它。 – Programmer

回答

0

問題是,當您使用var search = JsonConvert.DeserializeObject<dynamic>(resp.Content);時,您並未將其反序列化爲特定對象,並且很難打印出您的json。

如果您知道Json的外觀,請使用this將其轉換爲您可以輕鬆用於在屏幕上顯示Json的對象。請注意,您必須刪除{ get; set; }並將[Serializable]添加到每個生成的類的頂部,如here所述。

有了這些生成的類,我們也可以將收到的JSON來對象

//Convert Json to Object so that we can print it 
string yourJsonFromServer = resp.Content;//Replace with Json from the server 
RootObject rootObj = JsonUtility.FromJson<RootObject>(yourJsonFromServer); 

現在,串聯需要顯示的所有字符串。

string dispStr; 
dispStr = "__class__: " + rootObj.__class__ + "\r\n"; 
dispStr = dispStr + "mpn:" + rootObj.mpn + "\r\n"; 
dispStr = dispStr + "uid:" + rootObj.uid + "\r\n"; 

//manufacturer info 
dispStr = "Manufacturer __class__: " + rootObj.manufacturer.__class__ + "\r\n"; 
dispStr = "Manufacturer homepage_url: " + rootObj.manufacturer.homepage_url + "\r\n"; 
dispStr = "Manufacturer name: " + rootObj.manufacturer.name + "\r\n"; 
dispStr = "Manufacturer uid: " + rootObj.manufacturer.uid + "\r\n"; 

最後,使用Text組件來顯示它們。一個Text組件就足夠了。只要使用「\r\n」將它們分開:

public Text infoText; 
... 
infoText.horizontalOverflow = HorizontalWrapMode.Overflow; 
infoText.verticalOverflow = VerticalWrapMode.Overflow; 
infoText.text = dispStr; 

對於列表或數組中的項目,你可以使用for循環完了完了並顯示出來。

string dispStr = ""; 
for (int i = 0; i < rootObj.offers.Count; i++) 
{ 
    dispStr = dispStr + "SKU: " + rootObj.offers[i].sku + "\r\n"; 
    dispStr = dispStr + "REGION: " + rootObj.offers[i].eligible_region + "\r\n\r\n\r\n"; 
} 
infoText.text = dispStr; 

完整的例子:

public Text infoText; 

void Start() 
{ 
    //Convert Json to Object so that we can print it 
    string yourJsonFromServer = resp.Content;//Replace with Json from the server 
    RootObject rootObj = JsonUtility.FromJson<RootObject>(yourJsonFromServer); 

    string dispStr; 

    dispStr = "__class__: " + rootObj.__class__ + "\r\n"; 
    dispStr = dispStr + "mpn:" + rootObj.mpn + "\r\n"; 
    dispStr = dispStr + "uid:" + rootObj.uid + "\r\n"; 

    //Example, Show manufacturer info 
    dispStr = "Manufacturer __class__: " + rootObj.manufacturer.__class__ + "\r\n"; 
    dispStr = "Manufacturer homepage_url: " + rootObj.manufacturer.homepage_url + "\r\n"; 
    dispStr = "Manufacturer name: " + rootObj.manufacturer.name + "\r\n"; 
    dispStr = "Manufacturer uid: " + rootObj.manufacturer.uid + "\r\n"; 

    //Display 
    infoText.horizontalOverflow = HorizontalWrapMode.Overflow; 
    infoText.verticalOverflow = VerticalWrapMode.Overflow; 
    infoText.text = dispStr; 
} 

生成的類:

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

[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 object 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 object on_order_eta; 
    public int? on_order_quantity; 
    public object order_multiple; 
    public object packaging; 
    public Prices prices; 
    public string product_url; 
    public Seller seller; 
    public string sku; 
} 

[Serializable] 
public class RootObject 
{ 
    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; 
} 
+0

哇,我真的很感激它。它的工作原理。所以,只有最後一個疑問,所以要訪問製造商類。我該怎麼辦? //我所嘗試的是 - var resp = client.Execute(req); \t \t \t string octojson = resp.Content; \t \t \t RootObject objR = JsonUtility.FromJson (octojson); \t \t \t string hey = objR.hits; \t \t \t價格hh = JsonUtility.FromJson (octojson); //錯誤找不到價格 –

+0

它始終是'RootObject objR = JsonUtility.FromJson (octojson);'而沒有別的。然後你可以從'objR'變量中訪問其餘的數據。我甚至在例子中這樣做了......'rootObj.manufacturer'或者'製造商製造商= rootObj.manufacturer' ...現在,你可以使用他'製造商'變量來訪問它的值,比如'manufacturer.uid'。 .... – Programmer

+0

好吧,明白了!它只是我有很多在我腦中運行,現在我有兩個這樣的類(因爲我發射另一個請求,並得到一個JSON,不同的領域),所以由於某種原因,只有該類的rootobjects被訪問,而不是新的。我實際上改變了一個「Root」和一個「RootObject」,仍然只有Root被系統識別。奇怪。 –

0

如果你想用這個JSON一些調試輸出 - 這將是更容易使用GUI.Label。這很容易設置,並且您可以根據字符串中的行數或字符數來管理標籤的大小。但由於性能不佳,這絕對不適合生產。

另一個簡單的方法是創建Unity UI畫布並添加一個包含Text元素的面板。文本元素具有Best Fit屬性,它允許您爲標籤設置最小和最大字體大小。因此,字體的大小將自動計算,以使文本適合文本元素的大小。 enter image description here 爲了接收文本元素上的點擊事件,您還可以添加一個Button元素。