【问题标题】:Unity MiniJSON: Can't deserialize WWW responseUnity MiniJSON:无法反序列化 WWW 响应
【发布时间】:2014-10-05 19:28:17
【问题描述】:

我正在发送一个 WWW 请求并以 JSON 格式获取正确的 txt 响应,但我不确定我在将 JSON 字符串反序列化为字典时做错了什么。这是代码:

IEnumerator requestScores(int level)
{
    WWW jsonScores = new WWW(requestScoresURL + level);
    yield return jsonScores; //Wait for download to complete
    float elapsedTime = 0.0f;
    while (!jsonScores.isDone)
    {
        elapsedTime += Time.deltaTime;
        if (elapsedTime >= 10.0f) break;
        yield return null;
    }

    if (!jsonScores.isDone || !string.IsNullOrEmpty(jsonScores.error))
    {
        Debug.LogError(string.Format("Fail Whale!\n{0}", jsonScores.error));
        yield break;
    }
    string response = jsonScores.text;
    Debug.Log(elapsedTime + " : " + response);


    // Here "search" gets null value 
    Dictionary<string, object> search = Json.Deserialize(response) as Dictionary<string, object>;

}

所以据我所知jsonScores.txt 被正确检索但Dictionary&lt;string,object&gt; search 显示为空,我做错了什么?

提前致谢!

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    好的,我发现那里出了什么问题,因为我得到了多行,因此需要反序列化为字典列表而不是单个字典。

    这是有效的代码:

    List <Dictionary<string, object>> list;
    

    ...

    list = Json.Deserialize(response) as List<Dictionary<string, object>>;
    
    foreach (Dictionary<string, object> row in list)
    {
        // Do whatever with row
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      相关资源
      最近更新 更多