• 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集合中每项单独转化成字典。
  • 相关阅读:
    excel如何设置输入数字后单元格自动填充颜色
    怎样在excel中添加下拉列表框
    System.Security.Cryptography.CryptographicException 出现了内部错误
    怎样让webservice在浏览器远程浏览时像在本地浏览一样有参数输入框
    EF实体框架常见问题
    jdk安装环境变量设置
    IIS网站部署注意点
    浏览器被hao.360.cn劫持怎么办
    python-字典
    python-list列表
  • 原文地址:https://www.cnblogs.com/sunet/p/3730112.html
Copyright © 2020-2023  润新知