【问题标题】:Print JSON to screen as a block of text in unity将 JSON 作为文本块统一打印到屏幕上
【发布时间】:2017-09-07 03:55:25
【问题描述】:

我正在使用 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);
}

【问题讨论】:

  • 你从哪里得到错误?请发布您的其余代码并向我们展示错误所在。
  • @Programmer 我试图在运行时创建一个 GUIText ,但它需要传递一个字符串。所以我想将 search["hits"] 转换为字符串。但是出现错误。所以我的目标是在屏幕上打印我所有的结果。
  • 你需要使用JsonUtility。请张贴你的 json 的样子,我会告诉你如何打印它。
  • 嗨,谢谢。所以我已经发布了我的 json,我只想在一行中显示结果数组中的内容。这可能吗?
  • 抱歉,您的 json 无效有效。自己检查here

标签: c# .net unity3d monodevelop


【解决方案1】:

问题在于,当您使用var search = JsonConvert.DeserializeObject&lt;dynamic&gt;(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 组件就足够了。只需使用“\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;
}

【讨论】:

  • 哇,我真的很感激。它有效。所以最后一个疑问,所以要访问制造商类..我该怎么办? //我尝试的是 -- var resp = client.Execute(req);字符串 octojson = resp.Content; RootObject objR = JsonUtility.FromJson(octojson);字符串嘿 = objR.hits;价格 hh = JsonUtility.FromJson (octojson); //错误找不到价格
  • 总是RootObject objR = JsonUtility.FromJson&lt;RootObject&gt;(octojson);,没有别的。然后,您可以从 objR 变量访问其余数据。我什至在示例中这样做了....rootObj.manufacturer 或只是Manufacturer manufacturer = rootObj.manufacturer...现在,您可以使用他的manufacturer 变量来访问它的值,例如manufacturer.uid.....
  • 好的,明白了!只是我脑子里有很多在运行,现在我有两个这样的类(因为我触发了另一个请求并获得了一个 json,不同的字段)所以出于某种原因,只有那个类的根对象被访问,而不是新的。我实际上将一个更改为“Root”,一个更改为“RootObject”,但系统仍然只能识别 Root。很奇怪。
  • 如果您触发另一个请求并希望从该请求中获取结果,您必须再次执行 RootObject objR = JsonUtility.FromJson&lt;RootObject&gt;(octojson) 以更新 objR 变量,然后使用它来访问您的新数据变量。跨度>
  • 我最好单独问一个问题,可以吗?除了最后一条评论外,您所有的答案都很清楚。我的请求被发送到不同的站点,因此每个 RootObjects 类变量都不同。所以我有两个类,分别是 Root 和 RootObj,但只有 Root 被识别。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多