我正在使用 Unity 并且有一个 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);
}
问题是,当你使用
var search = JsonConvert.DeserializeObject<dynamic>(resp.Content);
时,你并没有将其反序列化为特定的对象,并且很难打印你的json。
如果您知道 Json 是什么样子,则使用 this 将其转换为一个对象,您可以轻松地使用该对象在屏幕上显示 Json。请注意,您必须删除
{ get; set; }
并将 [Serializable]
添加到每个生成的类的顶部,如此处所述。
使用这些生成的类,您可以将接收到的 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
组件就足够了。只需使用“\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;
}