统一打印JSON到屏幕作为文本块

Print JSON to screen as a block of text in unity

我正在使用 unity3d,我有一个 JSON 对象。我可以使用 ob.name 等访问每个成员,但我希望在 运行 time.Similar 期间将这个反序列化块打印在我的屏幕上以显示搜索结果,所以我得到JSON 作为搜索的结果,我想在我的 screen.I 上显示它得到的错误是我无法打印对象,因为我使用了 (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 上看到,我只需要在 screen.Please note 上动态打印它们,结果的大小或数量在 运行 时间并且会vary.Any 帮助是 appreciated.So 我找到了这个,现在我把它显示在屏幕上了。 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);
}

如果您想使用此 json 进行一些调试输出 - 使用 GUI.Label 会更容易。它易于设置,您可以根据字符串中的行数或字符数来管理标签的大小。但它绝对不适合生产,因为性能不好。

另一种简单的方法是创建 Unity UI Canvas 并添加一个带有 Text 元素的面板。文本元素有一个 Best Fit 属性 允许您设置标签的最小和最大字体大小。因此将自动计算字体大小以使文本适合 Text 元素的大小。 为了在 Text 元素上接收点击事件,您还可以添加 Button 元素。

问题是,当您使用 var search = JsonConvert.DeserializeObject<dynamic>(resp.Content); 时,您没有将其反序列化为特定对象,并且很难打印您的 json。

如果您知道 Json 的样子,请使用 this to convert it to an Object that you can easily use to display the Json on the screen. Note that you must remove { get; set; } and add [Serializable] to the top of each generated class as described

利用生成的类,你可以将接收到的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 component to display them. One Text组件就足够了。只需使用“\r\n”分隔它们:

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