前言:
- JSON(JavaScript ObjectNotation):是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。
具体的JSON数据格式内容如:
[{"id":"5","version":"1.0","name":"Android"},{"id":"6","version":"2.0","name":"JAVA"},{"id":"7","version":"3.0","name":"JavaScript"}]
其中: 中括号表示数据,大括号表示对象,分号用来分隔键和值,逗号用来分隔一个对象中的多个键值对。
Json数组:[ ]
Json对象:{ }
- Gson简介:Gson是Google提供的用来在java对象和json数据之间进行映射的java类库,可以将一个JSON字符串转成一个java对象(反序列化),或者将java对象转成一个JSON字符串(序列化)。
再详细介绍一下序列化与反序列化
序列化:把对象转换为字节序列的过程称为对象的序列化。(换句话说就是:把网络上传输的对象来转换为字节序列,意思就是把JavaBean对象转换为String的字符串,String已经实现了Serializable接口,已经实现序列化了)
反序列化:把字节序列恢复为对象的过程称为对象的反序列化。(是把网络上传输的序列化String字符串,恢复成JavaBean对象,就是将String的字符串转换为JavaBean对象)
在Android 下build.gradle下引入依赖:
implementation ‘com.google.code.gson:gson:2.8.6’
接下来用一个使用Gson将Student类型对象进行序列化,与反序列
1.Student类进行初始化:
package com.example.spdemo.bean; public class Student { private String name; private String sex; private int age; private String hobby; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } public Student(String name, String sex, int age, String hobby) { this.name = name; this.sex = sex; this.age = age; this.hobby = hobby; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + ", hobby='" + hobby + '\'' + '}'; } }
2.之后在MainActivity中,进行序列化与反序列化
Student student = new Student("小张","男",20,"读书"); //声明Gson对象 Gson gson = new Gson(); //序列化student对象 序列化:将对象转化为json字符串 String s = gson.toJson(student); System.out.println(s); //反序列化 反序列化:将json字符串重新恢复成对象 Student s1 = gson.fromJson(s,Student.class); System.out.println("姓名:"+s1.getName()+"性别:"+s1.getSex()+"年龄:"+s1.getAge()+"爱好:"+s1.getHobby());
后台输出:
使用Gson,将json字符串,序列化为Map集合
public void testJsonToMap() { String jsonString = "{\"my name\":\"tom\",\"1\":12}"; Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<Map<String, Object>>() { }.getType()); // Log.i(TAG, "testJsonToMap: " + map.toString()); Set<String> keySet = map.keySet(); for (String key : keySet) { Log.i(TAG, key + ":" + map.get(key)); } }
以上就是简单的使用Gson进行序列化与反序列化。
1、使用Android原生API,将json字符串反序列化为java对象
public void jsonTest() { String jsonString = "{\"id\":2, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}"; try { JSONObject jsonObject = new JSONObject(jsonString); int id = jsonObject.getInt("id"); String name = jsonObject.getString("name"); double price = jsonObject.getDouble("price"); String imagePath = jsonObject.getString("imagePath"); ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath); Log.i(TAG, "jsonTest: " + shopInfo.toString()); } catch (JSONException e) { e.printStackTrace(); } }
2、使用Android原生API,将json字符串反序列化为List集合
private void testJsonToList() { String jsonString = "[{\"id\":2, \"name\":\"大虾\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}," + "{\"id\":5, \"name\":\"鲍鱼\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f2.jpg\"}]"; List<ShopInfo> shopInfoList = new ArrayList<>(); //1.将json字符串包装JSONArray对象 try { JSONArray jsonArray = new JSONArray(jsonString); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); int id = jsonObject.getInt("id"); String name = jsonObject.getString("name"); double price = jsonObject.getDouble("price"); String imagePath = jsonObject.getString("imagePath"); ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath); shopInfoList.add(shopInfo); } Log.i(TAG, "testJsonToList: " + shopInfoList); } catch (JSONException e) { e.printStackTrace(); } }