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

问题描述 投票:0回答:2

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


  
c# .net unity-game-engine
2个回答
0
投票

问题是,当你使用

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

0
投票

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

另一种简单的方法是创建 Unity UI Canvas 并添加带有 Text 元素的面板。文本元素有一个

Best Fit
属性,允许您设置标签的最小和最大字体大小。因此,系统会自动计算字体大小,使文本适合 Text 元素的大小。 enter image description here 为了接收 Text 元素上的点击事件,您还可以添加 Button 元素。

© www.soinside.com 2019 - 2024. All rights reserved.