欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【安卓移动应用】反序列化别名与数据

程序员文章站 2022-03-16 07:57:02
...

在NuGet中安装Newtonsoft.Json

【安卓移动应用】反序列化别名与数据

创建用于返回消息到App类,JsonProperty设置小写别名

public class JsonReturn
    {
        /// <summary>
        /// 代码消息状态
        /// </summary>
        [JsonProperty("code")]//设置别名
        public int Code { get; set; }
        /// <summary>
        /// 文本消息
        /// </summary>
        [JsonProperty("text")]//设置别名
        public string Text { get; set; }
        /// <summary>
        /// 返回消息时额外携带的数据
        /// </summary>
        [JsonProperty("data")]//设置别名
        public object Data { get; set; }
    }

创建成员类,JsonProperty设置小写别名

public class MemberVo
    {
        [JsonProperty("memberId")]
        public int MemberId { get; set; }

        [JsonProperty("memberName")]
        public string MemberName { get; set; }

        [JsonProperty("memberTypeId")]
        public int? MemberTypeId { get; set; }

        [JsonProperty("checkInNum")]
        public int? CheckInNum { get; set; }

        [JsonProperty("phone")]
        public string Phone { get; set; }

        [JsonProperty("memberPassword")]
        public string MemberPassword { get; set; }

        [JsonProperty("registerTime")]
        public DateTime? RegisterTime { get; set; }

        [JsonProperty("certificateTypeId")]
        public int? CertificateTypeId { get; set; }

        [JsonProperty("certificateNumber")]
        public string CertificateNumber { get; set; }

        [JsonProperty("sex")]
        public bool? Sex { get; set; }

        [JsonProperty("photo")]
        public string Photo { get; set; }

        [JsonProperty("email")]
        public string Email { get; set; }


        //扩展
        [JsonProperty("memberType")]
        public string MemberType { get; set; }

        [JsonProperty("discount")]
        public decimal? Discount { get; set; }

        [JsonProperty("lastReserveTime")]
        public DateTime? LastReserveTime { get; set; }

        [JsonProperty("delayedCheckOutTime")]
        public DateTime? DelayedCheckOutTime { get; set; }

        [JsonProperty("breakfast")]
        public string Breakfast { get; set; }

        [JsonProperty("explains")]
        public string Explains { get; set; }
    }

使用Newtonsoft.Json反序列化返回

/// <summary>
        /// 登录、密码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ActionResult LoginByPassword(string phone, string password)
        {
            JsonReturn jsonReturn = new JsonReturn();

            if (!string.IsNullOrEmpty(phone) && phone.Length == 11)
            {
                MemberVo memberVo = (from tabMember in myModel.Member
                                     join tabMemberType in myModel.MemberTypes on tabMember.MemberTypeId equals tabMemberType.MemberTypeId
                                     where tabMember.Phone == phone
                                     select new MemberVo
                                     {
                                         MemberId = tabMember.MemberId, //会员ID
                                         MemberName = tabMember.MemberName, //会员名称
                                         MemberTypeId = tabMember.MemberTypeId, //会员类型ID
                                         CheckInNum = tabMember.CheckInNum, //入住次数
                                         Phone = tabMember.Phone, //手机号
                                         MemberPassword = tabMember.MemberPassword, //密码
                                         RegisterTime = tabMember.RegisterTime, //注册时间
                                         CertificateTypeId = tabMember.CertificateTypeId, //证件类型
                                         CertificateNumber = tabMember.CertificateNumber, //证件号
                                         Sex = tabMember.Sex, //性别
                                         Photo = tabMember.Photo, //手机号
                                         Email = tabMember.Email, //邮箱

                                         //扩展字段
                                         MemberType = tabMemberType.MemberType, //会员类型
                                         Discount = tabMemberType.Discount, //折扣
                                         LastReserveTime = tabMemberType.LastReserveTime, //最晚预留时间
                                         DelayedCheckOutTime = tabMemberType.DelayedCheckOutTime, //延迟退房时间
                                         Breakfast = tabMemberType.Breakfast, //早餐
                                         Explains = tabMemberType.Explains, //说明
                                     }).SingleOrDefault();
                if (memberVo != null)
                {
                    //获取数据库中的密码
                    string strPassword = memberVo.MemberPassword;

                    if (strPassword == password)
                    {
                        jsonReturn.Code = 200;
                        jsonReturn.Text = "登录成功";
                        jsonReturn.Data = memberVo;//将用户信息返回到app
                    }
                    else
                    {
                        jsonReturn.Code = 500;
                        jsonReturn.Text = "账号或密码不正确";
                    }
                }
                else
                {
                    jsonReturn.Code = 500;
                    jsonReturn.Text = "该手机号未注册";
                }
            }
            else
            {
                jsonReturn.Code = 500;
                jsonReturn.Text = "参数异常";
            }

            //使用 Newtonsoft.Json返回
            return Content(JsonConvert.SerializeObject(jsonReturn, Formatting.Indented, timeConverter));
        }

网络请求后对数据反序列化

//======发送网络请求======
OkHttpTool.httpPost(url, map, new OkHttpTool.ResponseCallback() {
    @Override
    public void onResponse(final boolean isSuccess,final int responseCode,final String response, Exception exception) {
        //更新UI在线程中执行
        myActivityContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String strText = "无法访问服务器,请稍后重试";
                if (isSuccess && responseCode == 200){
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        int code = jsonObject.getInt("code");
                        strText = jsonObject.getString("text");
                        if (code == 200){
                            Toast.makeText(myActivityContext, strText, Toast.LENGTH_LONG).show();
                            //对会员数据反序列化
                            String strData = jsonObject.getString("data");
                            MemberBean member = gson.fromJson(strData,MemberBean.class);
                            if (member != null){
                                //将member放入全局变量、内存
                                myApplication.setLoginMember(member);
                                //将phone和password存入SharedPreferences
                                SPUtils.put(myActivityContext,ServiceUrls.SP_MEMBER_PHONE,member.getPhone());
                                SPUtils.put(myActivityContext,ServiceUrls.SP_MEMBER_PASSWORD,member.getMemberPassword());

                                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                startActivity(intent);

                                //关闭当前Activity
                                finish();



//                                                //将phone和password存入SharedPreferences
//                                                //1、获取SharedPreferences对象
//                                                SharedPreferences sp = myActivityContext.getSharedPreferences("memberDate", Context.MODE_PRIVATE);
//                                                //2、获取SharedPreferences编辑器
//                                                SharedPreferences.Editor editor = sp.edit();
//                                                //3、存放数据
//                                                editor.putString("memberPhone",member.getPhone());
//                                                editor.putString("memberPassword",member.getMemberPassword());
//                                                //4、提交数据
//                                                editor.apply();
//
//                                                //读取SharedPreferences数据
//                                                String s1 = sp.getString("memberPhone","");
//                                                String s2 = sp.getString("memberPassword","");
//                                                Log.e("手机和密码",s1 + "\t" + s2);
                            }
                        }
                    } catch (JSONException e) {
                        //e.printStackTrace();
                        Toast.makeText(myActivityContext, "程序异常", Toast.LENGTH_LONG).show();
                    }
                }
                Toast.makeText(myActivityContext, strText, Toast.LENGTH_LONG).show();
            }
        });
    }
});
相关标签: 安卓