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
Your answer
Follow this Question
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