zoukankan      html  css  js  c++  java
  • 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 + " 体力, " + totalAwardGold + " 金币!";

           }

       }

     小小总结一下:其实第二种已经包含了第一种的情况,将award数组转化给List<object>泛型集合,数组每个元素{"num":14000,"propId":0,"type":2}看作一个object,很明显这样已经转化为了第一种情况了,再将List集合中每项单独转化成字典。
  • 相关阅读:
    AM8 自定义表情包的实现方法
    Create STKNetDiskC Instance Error
    怎样实现文件发文功能
    企业云盘的数据备份
    一种可行的文档协同编辑方法实现
    【OI】二分图最大匹配
    【OI】位运算操作
    【OI】关于快速幂的简单理解
    【OI】线性筛
    【OI】指针线段树&指针
  • 原文地址:https://www.cnblogs.com/sunet/p/3730112.html
Copyright © 2011-2022 走看看