Unity MiniJSON:无法反序列化WWW响应

时间:2014-10-05 19:28:18

标签: c# json unity3d

我发送了一个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<string,object> search出现为null,我做错了什么?

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

这是有效的代码:

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
}