I’m trying to deserialize a response to a WWW request like this:
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!
{0}", jsonScores.error));
yield break;
}
string response = jsonScores.text;
Debug.Log(elapsedTime + " : " + response);
Dictionary<string, object> search = Json.Deserialize(response) as Dictionary<string, object>;
//(1) IDictionary dict = (IDictionary) Json.Deserialize(response);
foreach ( KeyValuePair <string, object> entry in search)
{
Debug.Log("key: " + entry.Key + ", value:" + entry.Value);
// do something with entry.Value or entry.Key
}
}
So I’m getting a proper “jsonScores.text” but Dictionary “search” comes out as null.
If I use IDictionary ( Comment //(1) ) I get:
“InvalidCastException: Cannot cast from source type to destination type.”