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

问题描述 投票:0回答:1

我正在使用 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"]);                
} 

如果我不知道键名,如何保存键和值?

c# json unity-game-engine
1个回答
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());
    }            
}
© www.soinside.com 2019 - 2024. All rights reserved.