• unity用json和服务器数据交互


    第一种类型:服务器json数据是个对象

    /// <summary>
        /// 获取用户信息初始化信息
        /// </summary>
        void InitUserMessage()
        {
            //服务器ip端口和传入参数
            string path = string.Format("{0}/GetUserInfo?UserId={1}", PublicJs.url_RequestPrefix, PublicJs.GUID);
            StartCoroutine(GetUserInfo(path));
        }
        /// <summary>
        /// 从服务器获取数据
        /// </summary>
        /// <param name="urlParh">服务器ip和端口</param>
        /// <returns></returns>
        IEnumerator GetUserInfo(string urlParh)
        {
            Debug.Log(urlParh);
    
            DownloadItem down = new DownloadItem(urlParh);
            yield return down.www;
    
            string wwwText = down.Text();
            Debug.Log(wwwText);
            if (string.IsNullOrEmpty(wwwText) == false)
            {
                var curUser = Json.Deserialize(wwwText) as Dictionary<string, object>;
                view.InitUserMessage(curUser["UserName"].ToString(), curUser["ClassName"].ToString(), curUser["Coins"].ToString());
            }
            else
            {
                Debug.Log("信息错误");
            }
        }

    第二种类型:服务器数据是对象数组

    IEnumerator RefreshUIData()
        {
            string urlPath = string.Format("{0}/GetTrends?", PublicJs.url_RequestPrefix);
            DownloadItem down = new DownloadItem(urlPath);
            yield return down.www;
            string wwwText = down.Text();
            Debug.Log(urlPath);
            Debug.Log(wwwText);
            List<object> listObject = Json.Deserialize(wwwText) as List<object>;
    
            foreach (var i in listObject)
            {
                Dictionary<string, object> row = i as Dictionary<string, object>;
                HYDTAts hydt = new HYDTAts();
                hydt.ID = row["ID"].ToString();
                hydt.Name = row["Title"].ToString();
                hydtList.Add(hydt);
            }
    
            //hydtList = CSVFileHelp.GetCsvHYDT();
            //Debug.Log(hydtList.Count);
    
            int tp = 0;
            ItemHYDT itemHYDT;
            GameObject obj;
            foreach (HYDTAts item in hydtList)
            {
                obj = NGUITools.AddChild(view.GridObj, view.ItemObj);
                obj.name = tp.ToString("D3");
                obj.AddComponent<UIDragScrollView>();
                obj.transform.localPosition = new Vector3(0, -50 * tp, 0);
                itemHYDT = obj.AddComponent<ItemHYDT>();
                itemHYDT.SetLabelText(item.Name);
                itemHYDT.BindValue(item);
                itemHYDT.OnClick += ItemHYDT_OnClick;
                tp++;
            }
    
            yield return 0;
        }
  • 相关阅读:
    DBSCAN密度聚类
    特征工程之特征预处理
    特征工程之特征表达
    特征工程之特征选择
    Adaboost,GBDT和XGboost算法
    036 Go操作NSQ
    035 Go操作Redis
    034 Go操作MySQL
    033 protobuf初识
    032 二进制协议gob及msgpack介绍
  • 原文地址:https://www.cnblogs.com/unityzc/p/6801655.html
Copyright © 2020-2023  润新知