zoukankan      html  css  js  c++  java
  • flutter json转字符串 字符串转json

    一段json字符串

       var jsonStr = '{"errorCode": "0",     "message": "成功",     "data": {         "id": 25,         "appLoginUuid": "9c64754d293b487aa16fa266bf6aa9cf",         "userName": "18671045156",         "nickName": "18671045156",         "userType": "2",         "mobileNo": "18671045156",         "status": "3",         "userPhotoUrl": "/picture/logo.png",         "token": "5a7913fc62c974c8d8028a2ea58720d0aef07bba496e351744d3767d525456c4",         "downloadApp": false     } }';
    

    把json字符串转model

        //先转json
       var json = jsonDecode(jsonStr);
        //json转model
       var model = UserResModel.fromJson(json);
    
    

    存储

      void _saveUserInfo(UserInfo data) async {
        SharedPreferences pref= await SharedPreferences.getInstance();
        //将data转换成json字符串
        var str = json.encode(data);
         pref.setString(DataConst.KEY_UserInfo, str);
         _readUserInfo();
      }
    

    读取

     Future _readUserInfo()async{
          SharedPreferences pref= await SharedPreferences.getInstance();
         final jsonStr = pref.getString(DataConst.KEY_UserInfo);
         print(jsonStr);
         print(jsonStr.runtimeType);
         if(jsonStr != null && jsonStr.length > 0){
            //先将json字符串转json
           Map json = jsonDecode(jsonStr);
        //将json转model
          final model = UserInfo.fromJson(json);
            return model;    
         }
         return null;
      }
    
  • 相关阅读:
    hdu 1532(最大流)
    星沉月朗
    uva 818 (位运算 + 判环)
    SQL变量、Substring、charindex、case函数、去除重复
    C# Tostring格式
    asp.net导出word(word2007)
    asp.net生成缩略图
    正则表达式语法
    解压缩
    文件复制
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/11586088.html
Copyright © 2011-2022 走看看