我正在使用unity3d,我有一个JSON对象。我可以使用ob.name等访问每个成员,但我希望在运行时将这个反序列化的块打印在我的屏幕上。与搜索结果类似,所以我得到了搜索结果的JSON我想在我的屏幕上显示它。我得到错误,我无法打印对象,因为我使用(ob.name)toString(); 我不知道如何在屏幕上的运行时间显示它。
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上看到,我只需要在屏幕上动态打印它们。请注意,结果的大小或数量在运行时会有所不同。任何帮助都是值得赞赏的。所以我发现这个现在我在屏幕上看到它。 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 :(得分:0)
如果你想用这个json做一些调试输出 - 使用GUI.Label会更容易。它易于设置,您可以根据字符串中的行数或字符数管理标签的大小。但由于性能不佳,它绝对不适合生产。
另一种简单的方法是创建Unity UI Canvas并添加一个带有Text元素的Panel。 Text元素具有Best Fit
属性,允许您为标签设置最小和最大字体大小。因此,将自动计算字体的大小,以使文本适合Text元素的大小。
要在Text元素上接收单击事件,您还可以添加Button元素。
答案 1 :(得分:0)
问题在于,当您使用var search = JsonConvert.DeserializeObject<dynamic>(resp.Content);
时,您不会将其反序列化为特定的对象,并且很难打印您的json。
如果您知道Json的外观,请使用this将其转换为可以轻松用于在屏幕上显示Json的对象。请注意,您必须删除{ get; set; }
并将[Serializable]
添加到每个生成的类的顶部,如here所述。
使用这些生成的类,您可以将收到的Json转换为Object
//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
组件就足够了。只需使用&#34; \r\n
&#34;将它们分开:
public Text infoText;
...
infoText.horizontalOverflow = HorizontalWrapMode.Overflow;
infoText.verticalOverflow = VerticalWrapMode.Overflow;
infoText.text = dispStr;
对于List或Array项,您可以使用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;
}