問題是,當您使用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;
}
你從哪裏得到這個錯誤嗎?請張貼您的其他代碼,並告訴我們錯誤在哪裏。 – Programmer
@Programmer我試圖在運行時創建一個GUIText,但它需要傳遞一個字符串。所以我想將search [「hits」]轉換爲一個字符串。但是出現一個錯誤。所以我的目標是打印我所有的結果在屏幕上。 –
您需要使用JsonUtility。請張貼你的json的樣子,我會告訴你如何打印它。 – Programmer