[英]When I try to read the Json file I get the error "JSON parse error Invalid value"
我正在嘗試加載 Json 文件,但它給出了一個錯誤,在 Unity 編輯器中一切正常,但在 Android 中它給出了一個錯誤。 錯誤發生在下一行
WordsData loadedData = JsonUtility.FromJson<WordsData>(dataAsJson);
文件讀取 function
IEnumerator GetWordLevelData()
{
Debug.Log("1");
data = new Dictionary<string, string>();
Debug.Log("2");
string filePath;
Debug.Log("3");
filePath = Path.Combine(Application.streamingAssetsPath, language + "/" + LevelScrambled + ".json");
string dataAsJson;
Debug.Log("4");
if (filePath.Contains("://") || filePath.Contains(":///"))
{
Debug.Log("5");
UnityWebRequest www = UnityWebRequest.Get(filePath);
Debug.Log("6");
yield return www.Send();
Debug.Log("7");
dataAsJson = www.downloadHandler.text;
}
else
{
Debug.Log("8");
dataAsJson = File.ReadAllText(filePath);
}
Debug.Log("9");
WordsData loadedData = JsonUtility.FromJson<WordsData>(dataAsJson);
Debug.Log("10");
for (int i = 0; i < loadedData.items.Length; i++)
{
data.Add(loadedData.items[i].key, loadedData.items[i].value);
}
Debug.Log("11");
}
班級
[System.Serializable]
public class WordsData
{
public WordsItem[] items;
}
[System.Serializable]
public class WordsItem
{
public string key;
public string value;
}
Json 文件
{
"items":[
{
"key": "Mask",
"value": "ADULT"
},
{
"key": "Words",
"value": "dog/next/ant/top/"
},
{
"key": "LettersPerCost",
"value": "O1/G2/N1/E1/X3/T2/T1/O1/P3/"
}
]
}
此行中的錯誤:
filePath = Path.Combine(Application.streamingAssetsPath, language + "/" + LevelScrambled + ".json");
無法訪問 WebGL 和 Android 平台上的 StreamingAssets 文件夾。 WebGL 上沒有可用的文件訪問權限。 Android 使用壓縮的.apk 文件。 嘗試按路徑創建新文件,使用Application.persistentDataPath
。 例如:
private string GetJsonPath()
{
#if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID)
try
{
Directory.CreateDirectory(Application.persistentDataPath);
}
catch (Exception)
{
}
return Application.persistentDataPath + "/LevelScrambled.json";
#else
try
{
Directory.CreateDirectory(Application.streamingAssetsPath);
}
catch(Exception)
{
}
return Application.streamingAssetsPath + "/LevelScrambled.json";
#endif
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.