Flutter实战——Map Json Object对象转换

最近在写Flutter代码,发现iOS侧使用的是字典类型(Map),而我们Android使用的是对象类型。
数据类型完全不同,这可咋整?
于是研究了一下Map相关的Json转换,希望能帮助到大家

本篇Gson使用的是最新的版本:Gson Github

implementation 'com.google.code.gson:gson:2.8.6'

文章目录

1 只涉及基本数据类型

Map转json json转Map ——Map数据保持不变。

        Map<String, Object> map = new HashMap<>();
        map.put("string", "string");
        map.put("int", 0);
        map.put("double", 1.10d);
        map.put("float", 1.0f);
        map.put("boolean",false);
        map.put("char",'a');

        Log.i("TAG", "onCreate: map = " + map.toString());

        Gson gson = new Gson();
        String json = gson.toJson(map);
        Log.i("TAG", "onCreate: json = " + json);

        Map<String, Object> hashMap = gson.fromJson(json, HashMap.class);
        Map<String, Object> map1 = gson.fromJson(json, Map.class);

        Log.i("TAG", "onCreate: map1 = " + map1.toString());
        Log.i("TAG", "onCreate: hashMap = " + hashMap.toString());
onCreate: map = { boolean=false, string=string, double=1.1, char=a, float=1.0, int=0}
onCreate: json = { "boolean":false,"string":"string","double":1.1,"char":"a","float":1.0,"int":0}
onCreate: map1 = { boolean=false, string=string, double=1.1, char=a, float=1.0, int=0.0}
onCreate: hashMap = { boolean=false, string=string, double=1.1, char=a, float=1.0, int=0.0}

2 引用类型

2.1 简单的对象

Map转Json Json转Map ——Map中的对象变成了Map

public class Person { 
    public int age = 0;
    public String name = "liangchaojie";
}
        Map<String, Object> map = new HashMap<>();
        Person person0 = new Person();
        person0.age = 18;
        person0.name = "如花";
        map.put("object", person0);

        Log.i("TAG", "onCreate: map = " + map.toString());

        Gson gson = new Gson();
        String json = gson.toJson(map);
        Log.i("TAG", "onCreate: json = " + json);

        Person person = gson.fromJson(json, Person.class);
        Map<String, Object> hashMap = gson.fromJson(json, HashMap.class);
        Map<String, Object> map1 = gson.fromJson(json, Map.class);

        Log.i("TAG", "onCreate: person = " + person.toString());
        Log.i("TAG", "onCreate: map1 = " + map1.toString());
        Log.i("TAG", "onCreate: hashMap = " + hashMap.toString());

我们发现:

  • 居然可以转成Person类
  • 和之前的Map不一样了,Gson帮我们把这个Person对象转成了Map
onCreate: map = { object=com.qixuepai.bean.Person@4140e3}
onCreate: json = { "object":{ "age":18,"name":"如花"}}
onCreate: person = com.qixuepai.bean.Person@e03e95b
onCreate: map1 = { object={ age=18.0, name=如花}}
onCreate: hashMap = { object={ age=18.0, name=如花}}

转换之前我们 map.get(“object”).age

转换之后我们 map.get(“object”).get(“age”)

2.2 复杂的对象

Map转Json Json转Map ——Map中的对象变成了Map

public class Person { 

    public Map<String, Object> map = new HashMap<>();
    public List<String> list = new ArrayList<>();
    
}

 Map<String, Object> map = new HashMap<>();
 Person person0 = new Person();
 person0.map.put("key","value");
 person0.list.add("hhh");
 person0.list.add("呵呵呵");
 map.put("object", person0);
 
 //打印代码就不贴了,和上面的打印一样

onCreate: map = { object=com.qixuepai.bean.Person@4140e3}
onCreate: json = { "object":{ "list":["hhh","呵呵呵"],"map":{ "key":"value"}}}
onCreate: person = com.qixuepai.bean.Person@f6d8ad1
onCreate: map1 = { object={ list=[hhh, 呵呵呵], map={ key=value}}}
onCreate: hashMap = { object={ list=[hhh, 呵呵呵], map={ key=value}}}
onCreate: object = { list=[hhh, 呵呵呵], map={ key=value}}

3 分析

现在我们的认知里出现了两种map:

  1. {object=com.qixuepai.bean.Person@4140e3} map存放的是对象,多用于java代码
  2. {object={list=[hhh, 呵呵呵], map={key=value}}} map存放的是map字典,多用于ios侧

所以现在的矛盾就在于:

Flutter的Map是什么?很遗憾,大部分情况下,Flutter Map是采用ios侧的字典类型而不是android侧的对象类型

可是你本地确实需要对象类型的数据,怎么办?
那就必须进行字典->对象 对象->字典的转换

3.1 字典到对象

字典->json json->对象


 Log.i("TAG", "onCreate: map1 = " + map1.toString());
 String json1 = gson.toJson(map1);
 Log.i("TAG", "onCreate: json1 = " +json1);
 Person person = gson.fromJson(json1, Person.class);
 Log.i("TAG", "onCreate: person = " +person);
 

onCreate: map1 = { object={ list=[hhh, 呵呵呵], map={ key=value}}}
onCreate: json1 = { "object":{ "list":["hhh","呵呵呵"],"map":{ "key":"value"}}}
onCreate: person = com.qixuepai.bean.Person@975c9a4

3.1 对象到字典

对象->json json->字典


        Person person0 = new Person();
        person0.map.put("key","value");
        person0.list.add("hhh");
        person0.list.add("呵呵呵");

        Gson gson = new Gson();
        String json = gson.toJson(person0);
        Log.i("TAG", "testPrint: person "+person0);

        Map<String, Object> map1 = gson.fromJson(json, Map.class);
        Log.i("TAG", "testPrint: map1 "+map1);
        

需要注意的时候,如果某个字段的值为null,那么这个字段就不会映射到字典中。

这样就把Map的数据传递讲清楚了。

这里也列举一下之前写的博客:

json字符串与对象之间转换,字段一致和不一致的情况

利用GsonFormat+FastJson对复杂json数据格式进行转换

Retrofit+fastjson

    原文作者:Gerry_Liang
    原文地址: https://blog.csdn.net/LosingCarryJie/article/details/107946724
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞