统一将JSON打印到屏幕上作为文本块 [英] Print JSON to screen as a block of text in unity

查看:103
本文介绍了统一将JSON打印到屏幕上作为文本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用unity3d,并且有一个JSON对象.我可以使用ob.name等访问每个成员,但是我希望在运行时将这个反序列化的块打印在屏幕上.类似于搜索结果,因此我得到了搜索和搜索结果的JSON.我想在屏幕上显示它.由于我使用(ob.name)toString();而出现无法打印对象的错误. 我不确定如何在运行时在屏幕上显示它.

I am using unity3d and I have a JSON object. I am able to access each member using ob.name and so on but I am looking to get this deserialized block to be printed on my screen during run time.Similar to a search result , so I get the JSON as a result of search and I want to display it on my screen.I get errors that I cannot print the object because I used (ob.name)toString(); I am not sure how to display this during run time on the screen.

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有人帮助我如何让每个人都响应点击事件吗?

EDIT : I am able to see on Debug.Log , I just need to dynamically print them on screen.Please note , the size or number of results is on runtime and will vary.Any help is appreciated.So I found this and now I get it on the screen. http://wiki.unity3d.com/index.php/DebugConsole.Can anybody help on how I can make each respond to click events ?

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);时,没有将其反序列化为特定的Object,并且很难打印json.

The problem is that when you use var search = JsonConvert.DeserializeObject<dynamic>(resp.Content);, you are not de-serializing it to a specific Object and it is hard to print your json.

如果您知道Json的外观,请使用将其转换为可以轻松使用的对象在屏幕上显示Json.请注意,您必须删除{ get; set; }并将[Serializable]添加到每个生成的类的顶部,如

If you know what the Json looks like then use 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 here.

有了这些生成的类,您可以将收到的Json转换为Object

With those generated classes, you can convert the received Json to 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);

现在,将您需要显示的所有字符串连接起来.

Now, concatenate all the strings you need to display.

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"将它们分开:

Finally, use the Text component to display them. One Text component is enough for this. Just use "\r\n" to separate them:

public Text infoText;
...
infoText.horizontalOverflow = HorizontalWrapMode.Overflow;
infoText.verticalOverflow = VerticalWrapMode.Overflow;
infoText.text = dispStr;

对于列表或数组项目,您可以仅使用for循环来显示并显示它们.

For List or Array items, you can just use for loop to over over and display them.

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;


完整示例:


Complete Example:

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

这篇关于统一将JSON打印到屏幕上作为文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆