如何在unity3d中使用minijson打印键和值

时间:2013-08-14 05:16:26

标签: c# json unity3d monodevelop

我正在使用minijson从unity3d中的json获取值

回复是

{"existing":[{"LastUpdated":"7\/9\/2013 4:03:57 AM","LetterSpeed":100,"Letters":"SP","NumRandom":5,"Reward":10,"ShowWordDesc":false,"ShowWordHint":false,"Sort_ID":5,"WN_Level_ID":34,"WN_Word_ID":95,"WordDesc":"","WordHint":"","zError":null},{"LastUpdated":"7\/9\/2013 4:03:57 AM","LetterSpeed":100,"Letters":"aún","NumRandom":2,"Reward":10,"ShowWordDesc":true,"ShowWordHint":false,"Sort_ID":10,"WN_Level_ID":34,"WN_Word_ID":83,"WordDesc":"still","WordHint":"","zError":null},{"LastUpdated":"7\/9\/2013 4:03:58 AM","LetterSpeed":200,"Letters":"tanto","NumRandom":3,"Reward":10,"ShowWordDesc":true,"ShowWordHint":false,"Sort_ID":20,"WN_Level_ID":34,"WN_Word_ID":84,"WordDesc":"so","WordHint":"","zError":null}]}

我使用的代码是

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using MiniJSON;

在功能中

string response = www.text; 
Debug.Log(response);            
IDictionary search = (IDictionary) Json.Deserialize(response);
IList tweets = (IList) search["existing"];      
foreach (IDictionary tweet in tweets) {
    Debug.Log(tweet["LastUpdated"]);                
} 

如果我不知道密钥名称,我如何保存密钥和值?

1 个答案:

答案 0 :(得分:0)

不完全确定你在这里输出的是什么,但是如果我想猜测你想要做以下事情:

string response = www.text; 

Debug.Log(response); 

IDictionary search = (IDictionary) Json.Deserialize(response);

IList tweets = (IList) search["existing"];  

foreach (IDictionary tweet in tweets) {
    Debug.Log("Tweet details:");
    foreach( KeyValuePair<string, string> tweetData in tweet )  {
        Debug.Log("Key = " + tweetData.Key.ToString() +
                  " Value = " + tweetData.Value.ToString());
    }            
}