最近在写Flutter代码,发现iOS侧使用的是字典类型(Map),而我们Android使用的是对象类型。
数据类型完全不同,这可咋整?
于是研究了一下Map相关的Json转换,希望能帮助到大家
本篇Gson使用的是最新的版本:Gson Github
implementation 'com.google.code.gson:gson:2.8.6'
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}
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());
我们发现:
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”)
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}}
现在我们的认知里出现了两种map:
所以现在的矛盾就在于:
Flutter的Map是什么?很遗憾,大部分情况下,Flutter Map是采用ios侧的字典类型而不是android侧的对象类型
可是你本地确实需要对象类型的数据,怎么办?
那就必须进行字典->对象 对象->字典的转换
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
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 map1 = gson.fromJson(json, Map.class);
Log.i("TAG", "testPrint: map1 "+map1);
这样就把Map的数据传递讲清楚了。
这里也列举一下之前写的博客:
json字符串与对象之间转换,字段一致和不一致的情况
利用GsonFormat+FastJson对复杂json数据格式进行转换
Retrofit+fastjson