JSON字符串如何转换为实体类对象
https://blog.csdn.net/wkw1598727534/article/details/107104361/
一、整体代码压缩包
二、实现步骤
1、定义SharedPreferences成员变量
2、定义SharedPreferences局部变量
3、将实体类放入,然后将实体类转换成字符串类型
4、获取数据(id是int类型,所以将int转换成字符串)
三、整体代码:
1.HomePageActivity文件代码
package com.example.ok;import androidx.appcompat.app.AppCompatActivity;import android.content.SharedPreferences;import android.os.Bundle;import android.widget.TextView;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.example.ok.bean.UserInfoBean;public class HomePageActivity extends AppCompatActivity {private TextView tv_home;SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_home_page);tv_home=findViewById(R.id.tv_home);initData();}public void initData(){sp=getSharedPreferences("data",MODE_PRIVATE);String userInfoBean = sp.getString("userInfoBean", null);JSONObject userJson = JSONObject.parseObject(userInfoBean);UserInfoBean user = JSON.toJavaObject(userJson,UserInfoBean.class);tv_home.setText(String.valueOf(user.getData().getUser().getId()));}}
2.结果图:
