![](/img/trans.png)
[英]C#: KeyNotFoundException: The given key was not present in the dictionary
[英]KeyNotFoundException: The given key was not present in the dictionary (C# Unity ANDROID APP)
我知道我的书名重复了很多,但似乎找不到与我相同的问题。 让我粘贴我的代码。
rh.eLanguage language = tzGlobal.Instance.OPTION.language;
string json = StreetUtility.LoadJsonFromStreamingAssets("notice.json");
if (json != null)
{
// Separate only the necessary parts.
LitJson.JsonData data = LitJson.JsonMapper.ToObject(json);
json = data[language.ToString()].ToJson();
notice = LitJson.JsonMapper.ToObject<string[]>(json);
}
string path = string.Format("{0}/{1}/language", rh.Const.LOCALIZATION_PATH, language);
json = StreetUtility.LoadJsonFromResources(path);
if (json != null)
{
// json load.
Dictionary<string, string> dic = LitJson.JsonMapper.ToObject<Dictionary<string, string>>(json);
// dictionary copy.
dic_localization_text = new Dictionary<eTextKey, string>();
eTextKey e;
for (int i = 0; i < dic.Count; i++)
{
e = (eTextKey)i;
dic_localization_text[e] = dic[e.ToString()];
}
// Run registered localize function.
for (int i = 0; i < list_localize_method.Count; i++)
{
complete = false;
list_localize_method[i].Invoke();
yield return new WaitUntil(() => complete);
}
}
Debug.LogWarning("TODO: Loading popup off.");
注意:我有2个版本(PC版本)和(移动版本),PC版本工作得很好,现在我的问题是移动版本
注意: 这是一个android应用程序(apk)
我在LocalizationManager
上所做的是从我的流媒体资产(( notice.json
)中加载json文件,但是问题是在我的logcat上有此错误
:KeyNotFoundException:给定的密钥在System.Collections.Generic.Dictionary`2 [System.String,LitJson.JsonData] .get_Item(系统字符串字符串)[0x00000]中位于LitJson.JsonData的:0中不存在。 .get_Item(System.String prop_name)[0x00000]在0时在LocalizationManager + c__Iterator0.MoveNext()在[0x00000]在0时在UnityEngine.SetupCoroutine.InvokeMoveNext(IEnumerator枚举器,IntPtr returnValueAddress)[0x00000]在0时
也许你会问我在做什么。 我正在执行Choose Language Scenario
。
如您在图像上看到的,如果我选择韩国,则必须更改为韩国,依此类推。
有关更多信息,请StreetUtility.LoadJsonFromStreamingAsset
我的StreetUtility.LoadJsonFromStreamingAsset
函数
public static string LoadJsonFromStreamingAssets(string path_with_extention_under_streaming_assets_folder)
{
string json = null;
try
{
//Android Platform
#if UNITY_ANDROID
string full_path = Application.persistentDataPath + path_with_extention_under_streaming_assets_folder;
WWW reader = new WWW(full_path);
while (!reader.isDone) { }
json = reader.text;
Debug.Log("Loaded Files: " + json);
#elif UNITY_IOS //IOS Platform
#elif UNITY_STANDALONE //PC Platform
string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
StreamReader reader = new StreamReader(full_path);
json = reader.ReadToEnd().Trim();
reader.Close();
Debug.Log(json);
#endif
}
catch (Exception e)
{
Debug.LogWarningFormat("Failed to Load.\n{0}\n{1}", e, path_with_extention_under_streaming_assets_folder);
}
return json;
}
这行代码也与错误有关。 我很确定
#if UNITY_ANDROID
string full_path = Application.persistentDataPath + path_with_extention_under_streaming_assets_folder;
WWW reader = new WWW(full_path);
while (!reader.isDone) { }
json = reader.text;
Debug.Log("Loaded Files: " + json);
提前非常感谢您,如果您不懂我的英语,我会深表歉意。 请问。 再次感谢你。
我将与您分享这里真正的错误是什么。 我没什么错
下面的代码行运行正常。 因此,让我通过添加注释行来解释一下
LocalizationManager.cs
//Convert the json string into a LitJson JSON Object
LitJson.JsonData data = LitJson.JsonMapper.ToObject(json);
//Take the json sub-object mapped to whatever language is represented by this variable
json = data[language.ToString()].ToJson();
//and convert this sub-object to an array of strings
notice = LitJson.JsonMapper.ToObject<string[]>(json);
所以现在我的问题出在StreetUtility.cs上,其中我更改了以下代码行:
string full_path = Application.persistentDataPath + path_with_extention_under_streaming_assets_folder;
WWW reader = new WWW(full_path);
while (!reader.isDone) { }
json = reader.text;
Debug.Log("Loaded Files: " + json);
到这行代码
string full_path = Path.Combine(Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
if (full_path.Contains("://") || full_path.Contains(":///"))
{
WWW www = new WWW(full_path);
while (!www.isDone) { }
json = www.text.Trim();
}
else
{
json = File.ReadAllText(full_path);
}
Debug.Log("Loaded file: " + json);
所以我需要在json = www.text.Trim()
Trim()
上放一个Trim()
,因为如果我不放Trim()
,它将导致一个异常说
JsonException:输入字符串中的无效字符''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.