Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by GuardFromUA · Dec 12, 2017 at 11:02 AM · uijsonrest

Pasting data from JSON into UI

Followed some tutorial and can't paste data from JSON into my UI.
What I have: Countries class is used for making WWW request and downloading JSON. In that class, I have to generate UI elements from prefab and set image from it and text(for example, some images, that have TEXT attach to the and this text contains country name fields)
The Helpers class is where our data would be stored + Jsonhelper class, that will use Wrapper.
Plz help me with this.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using System.Linq;
 using System.Linq.Expressions;
 
 public class Countries : MonoBehaviour
 {
     //   public UnityEngine.UI.Dropdown ddlCountries;
 public GameObject some_prefab;
     // Use this for initialization
     void Start()
     {
 
         StartCoroutine(GetCountries());
     }
 
 
     IEnumerator GetCountries()
     {
         string getCountriesUrl = "https://restcountries.eu/rest/v2/all";
         using (UnityWebRequest www = UnityWebRequest.Get(getCountriesUrl))
         {
             //www.chunkedTransfer = false;
             yield return www.Send();
             if (www.isNetworkError || www.isHttpError)
             {
                 Debug.Log(www.error);
             }
             else
             {
                 if (www.isDone)
                 {
                     string jsonResult =
                         System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
                       Debug.Log(jsonResult);
                     RootObject[] entities =
                         JsonHelper.getJsonArray<RootObject>(jsonResult);
 // here I have to instantiate my prefab  and set text in this prefab to strings from JSON
                 }
                     
                 }
                
             }
         }
     }

HELPER:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class JsonHelper
 {
     public static T[] getJsonArray<T>(string json)
     {
         string newJson = "{ \"array\": " + json + "}";
         Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
         return wrapper.array;
     }
 
     [System.Serializable]
     private class Wrapper<T>
     {
         // This public array will always throw a warning
         // Assets/Scripts/Helpers.cs(21,20): warning CS0649: Field `JsonHelper.Wrapper<T>.array' is never assigned to, and will always have its default value `null'
         // As the function works with no bugs, this can be made pragmatic.
 #pragma warning disable
         public T[] array;
 #pragma warning restore
     }
 }
 
 
 [System.Serializable]
 public class Currency
 {
     public string code;
     public string name;
     public string symbol;
 }
 [System.Serializable]
 public class Language
 {
     public string iso639_1;
     public string iso639_2;
     public string name;
     public string nativeName;
 }
 [System.Serializable]
 public class Translations
 {
     public string de;
     public string es;
     public string fr;
     public string ja;
     public string it;
     public string br;
     public string pt;
 }
 [System.Serializable]
 public class RootObject
 {
     public string name;
     public List<string> topLevelDomain;
     public string alpha2Code;
     public string alpha3Code;
     public List<object> callingCodes;
     public string capital;
     public List<object> altSpellings;
     public string region;
     public string subregion;
     public int population;
     public List<object> latlng;
     public string demonym;
     public double? area;
     public double? gini;
     public List<string> timezones;
     public List<object> borders;
     public string nativeName;
     public string numericCode;
     public List<Currency> currencies;
     public List<Language> languages;
     public Translations translations;
     public string flag;
     public List<object> regionalBlocs;
 }
 [System.Serializable]
 public class Main_menu
 {
     public string image;
     public string name;
   
 
 }
 
 [System.Serializable]
 public class CountriesList
 {
     public List<RootObject> Countries;
 }
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reading GeoJSON coordinates into Unity for Interactive 2D Map. 0 Answers

Refresh Chat room messages 1 Answer

UnityWebRequest read body response 0 Answers

Dynamic Menu C# 0 Answers

i cant put text into a ui text thru a c# script 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges