在做項目時,分別用到這兩種json數據,首先download一個minijson程序集吧,下面重點說下處理的方法。
1,{"result":1,"playerRank":2,"item":{"item1":1001,"item2":0,"item3":3,"item4":4}}
IEnumerator GetLoginData(string url)
{
WWW www = new WWW(url);
yield return www;
if (www.error != null)
{
StopCoroutine("GetLoginData");
}
else
{
Dictionary<string, object> dictJson = MiniJSON.Json.Deserialize(www.text) as Dictionary<string, object>;
Dictionary<string, object> dictLevels = dictJson["items"] as Dictionary<string, object>;
}
}
2,{"result":1,"name":"《小雞BIBI》漫寵實惠大禮包","award":[{"num":14000,"propId":0,"type":2},{"num":15,"propId":0,"type":3}]}
IEnumerator GetFMaReward(string url)
{
WWW www = new WWW(url);
yield return www;
if (www.error != null)
{
GlobalVariable.isConfirmUpLevel = true;
confirmBtnLodingF.SetActive(false);
goWarnFMaLbl.GetComponent<UILabel>().text = "F碼錯誤!";
Invoke("ClearWarnLbl", 4.0f);
StopCoroutine("GetFMaReward");
}
else
{
Dictionary<string, object> dictJson = MiniJSON.Json.Deserialize(www.text) as Dictionary<string, object>;
if (System.Convert.ToInt32(dictJson["result"]) == 1)
{
List<object> awardList = dictJson["award"] as List<object>;
int totalAwardGold = 0;
int totalAwardPower = 0;
foreach (var item in awardList)
{
Dictionary<string, object> dictAward = item as Dictionary<string, object>;
int type = Convert.ToInt32(dictAward["type"]);
//金幣獎勵
if (type == 2)
{
totalAwardGold += Convert.ToInt32(dictAward["num"]);
GlobalVariable.sg_GoldNum += Convert.ToInt32(dictAward["num"]);
}
//體力
if (type == 3)
{
totalAwardPower += Convert.ToInt32(dictAward["num"]);
GlobalVariable.sg_ChallengeNum += Convert.ToInt32(dictAward["num"]);
}
}
goGoldNum.GetComponent<UILabel>().text = GlobalVariable.sg_GoldNum.ToString();
goNowTime.GetComponent<NowTime>().showNum();
goExchangeLiBao.SetActive(false);
goFMaSuccess.SetActive(true);
goFMaSuccessLbl.GetComponent<UILabel>().text = "恭喜你獲得 " + totalAwardPower + " 體力,\r\n" + totalAwardGold + " 金幣!";
}
}
小小總結一下:其實第二種已經包含了第一種的情況,將award數組轉化給List<object>泛型集合,數組每個元素{"num":14000,"propId":0,"type":2}看作一個object,很明顯這樣已經轉化爲了第一種情況了,再將List集合中每項單獨轉化成字典。