MiniJson解析Json字符串

 在做项目时,分别用到这两种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集合中每项单独转化成字典。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章