【问题标题】:KeyNotFoundException: The given key was not present in the dictionary (C# Unity ANDROID APP)KeyNotFoundException:字典中不存在给定的键(C# Unity ANDROID APP)
【发布时间】:2018-08-14 01:07:44
【问题描述】:

我知道我的标题有很多重复项,但我似乎找不到与我相同的问题。让我粘贴我的代码。

LocalizationManager.cs

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 版运行良好,现在我的问题是移动版

注意: 这是一个安卓应用程序(apk)

我在LocalizationManager 上所做的是我正在从我的流媒体资产(notice.json)中加载 json 文件,但问题是在我的 logcat 上它有这个错误

:KeyNotFoundException: 给定的键不在dictionary.at System.Collections.Generic.Dictionary`2[System.String,LitJson.JsonData].get_Item (System.String key) [0x00000] in :0 在 LitJson.JsonData.get_Item (System.String prop_name) [0x00000] in :0 在 LocalizationManager+c__Iterator0.MoveNext () [0x00000] in :0 在 UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in :0

也许你会问我在做什么。我正在做一个Choose Language Scenario.

正如你在图片上看到的,如果我选择韩国,它必须更改为韩国等等。

更多信息在这里是我的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);

非常感谢您,如果您不懂我的英语,我会道歉。请问一下。再次感谢。

【问题讨论】:

标签: c# android unity3d


【解决方案1】:

我将与您分享这里真正的错误。我的没有问题

下面的代码行运行良好。所以让我通过注释行来解释一下

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(),它会导致异常提示

JsonException: 输入字符串中的无效字符 ''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多