Help to read Json on Android! Unity Android Game
Why my game reads well the Json (where I have the LocalizedText) in iOS, Windows, Mac .. but in android does not work? I use Unity's Localization Manager official tutorial : link
void Awake () {
if(instance == null)
{
instance = this;
}
else if(instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
LoadLocalizedText("localizedText_de.json");
}
public void LoadLocalizedText(string fileName)
{
localizedText = new Dictionary<string, string>();
string filePath = Path.Combine(Application.streamingAssetsPath, fileName);
if (File.Exists(filePath))
{
string dataJson = File.ReadAllText(filePath);
LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataJson);
for (int i = 0; i < loadedData.items.Length; i++)
{
localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
}
//Debug.Log("Localization data loaded : " + localizedText.Count + " items loaded.");
}
else
{
Debug.LogError("File not found !");
}
isReady = true;
}
public string GetLocalizedValue(string key)
{
string result = missingTextString;
if (localizedText.ContainsKey(key))
{
result = localizedText[key];
}
return result;
}
Please help !
Comment
Answer by ArminAhmadi · Jan 28, 2019 at 06:01 AM
I usually use LitJason myself and it works fine on android. I attach the scripts here for you to download. Import the scripts to your project and use this example to read json data from string.
private void stringToJson(string data)
{
JsonData jsonvale = JsonMapper.ToObject(data);
string whateverString = jsonvale["whateverString"].ToString();
int whateverInteger = int.Parse(jsonvale["whateverInteger"].ToString());
}
litjson.zip
(16.0 kB)