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.
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.
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";
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 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;
}
Generated classes:
[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;
}