概述
项目地址:https://github.com/wrs13634194612/Gson_jsonString.git
1.添加依赖:
implementation 'com.google.code.gson:gson:2.8.0'
2.UserSimple对象类:
package com.example.administrator.testz;
import com.example.administrator.testz.model.RestaurantMenuItem;
import java.util.HashMap;
import java.util.List;
/**
* Created by wrs on 2019/6/25,17:35
* projectName: Testz
* packageName: com.example.administrator.testz
*/
public class UserSimple {
// 通过快捷键Alt+Insert
String name;
String email;
int age;
boolean isDeveloper;
HashMap<String, String> emplyees;
public HashMap<String, String> getEmplyees() {
return emplyees;
}
public void setEmplyees(HashMap<String, String> emplyees) {
this.emplyees = emplyees;
}
public UserSimple(String mName, String mEmail, int mAge, boolean developer, HashMap<String, String> mEmplyees) {
this.name = mName;
this.email = mEmail;
this.age = mAge;
this.isDeveloper = developer;
this.emplyees = mEmplyees;
}
public UserSimple(String mName, String mEmail, int mAge, boolean developer) {
this.name = mName;
this.email = mEmail;
this.age = mAge;
this.isDeveloper = developer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isDeveloper() {
return isDeveloper;
}
public void setDeveloper(boolean developer) {
isDeveloper = developer;
}
}
3.RestaurantWithMenu对象类:
package com.example.administrator.testz.model;
import java.util.List;
/**
* Created by wrs on 2019/6/25,17:51
* projectName: Testz
* packageName: com.example.administrator.testz.model
*/
public class RestaurantWithMenu {
String name;
List<RestaurantMenuItem> menu;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<RestaurantMenuItem> getMenu() {
return menu;
}
public void setMenu(List<RestaurantMenuItem> menu) {
this.menu = menu;
}
public RestaurantWithMenu(String future, List<RestaurantMenuItem> menuItems) {
this.name = future;
this.menu = menuItems;
}
}
3.RestaurMenuItem 对象类:
package com.example.administrator.testz.model;
/**
* Created by wrs on 2019/6/25,17:51
* projectName: Testz
* packageName: com.example.administrator.testz.model
*/
public class RestaurantMenuItem {
String description;
float price_wrs;
public RestaurantMenuItem() {
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice_wrs() {
return price_wrs;
}
public void setPrice_wrs(float price_wrs) {
this.price_wrs = price_wrs;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String name;
public RestaurantMenuItem(String description, float price,String mName) {
this.description = description;
this.price_wrs = price;
this.name = mName;
}
public RestaurantMenuItem(String description, float price) {
this.description = description;
this.price_wrs = price;
}
}
4. 生成json 解析json
package com.example.administrator.testz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.administrator.testz.model.RestaurantMenuItem;
import com.example.administrator.testz.model.RestaurantWithMenu;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//参考网址: https://www.jianshu.com/p/9a4dcd436d7f
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//oneJson();
// twoJson();
// threeJson();
// fourJson(); 纯list数组
// fiveJson();
sixJson();
}
private void sixJson() {
HashSet<String> users = new HashSet<>();
users.add("Charistian");
users.add("Marcus");
users.add("Norman");
users.add("Marcus");
Gson gson = new Gson();
String res = gson.toJson(users);
System.out.println("sixJson:"+res);
/**
* ["Marcus", "Norman", "Charistian"]
*/
//解析 json
Type founderSetType = new TypeToken<HashSet<String>>(){}.getType();
HashSet<String> founderSet = gson.fromJson(res, founderSetType);
Iterator it = founderSet.iterator();
while(it.hasNext()){
System.out.println("res:"+it.next());
}
}
private void fiveJson() {
HashMap<String,List<String>> emplyees = new HashMap<>();
emplyees.put("D", Arrays.asList("Andress","arnold","Aden"));
emplyees.put("Q", Arrays.asList("Christian","carter"));
emplyees.put("W", Arrays.asList("Marcus","mary","tom","bob"));
Gson gson = new Gson();
String res = gson.toJson(emplyees);
System.out.println("fiveJson:"+res);
/**
{
"D": ["Andress", "arnold", "Aden"],
"W": ["Marcus", "mary", "tom", "bob"],
"Q": ["Christian", "carter"]
}
res:bob
*/
//解析
Type amountCurrencyType = new TypeToken<HashMap<String, List<String>>>(){}.getType();
HashMap<String, List<String>> amountCurrency =
gson.fromJson(res, amountCurrencyType);
System.out.println("res:"+amountCurrency.get("W").get(3));
}
private void fourJson() {
List<RestaurantMenuItem> menuItems = new ArrayList<>();
menuItems.add(new RestaurantMenuItem("zhaoyun",7.6f));
menuItems.add(new RestaurantMenuItem("haungchao",4.7f));
menuItems.add(new RestaurantMenuItem("liubei",9.2f));
Gson gson = new Gson();
String res = gson.toJson(menuItems);
System.out.println("fourJson:"+res);
//解析
Type type = new TypeToken<List<RestaurantMenuItem>>() {}.getType();
List<RestaurantMenuItem> list = gson.fromJson(res, type);
System.out.println("name:"+ list.get(0).getDescription());
/**
* [{
"description": "zhaoyun",
"price_wrs": 7.6
}, {
"description": "haungchao",
"price_wrs": 4.7
}, {
"description": "liubei",
"price_wrs": 9.2
}]
//解析
name:zhaoyun
*/
}
private void twoJson() {
List<RestaurantMenuItem> menuItems = new ArrayList<>();
menuItems.add(new RestaurantMenuItem("Spaghetti", 7.99f));
menuItems.add(new RestaurantMenuItem("steak", 12.99f));
menuItems.add(new RestaurantMenuItem("salad", 5.99f));
RestaurantWithMenu restaurantWithMenu = new RestaurantWithMenu("Future", menuItems);
Gson gson = new Gson();
String res = gson.toJson(restaurantWithMenu);
System.out.println("twojson: " + res);
//解析字符串
restaurantWithMenu = gson.fromJson(res, RestaurantWithMenu.class);
System.out.println("name:"+ restaurantWithMenu.getName());
for (int i=0;i<restaurantWithMenu.getMenu().size();i++){
System.out.println(i+" :list: "+restaurantWithMenu.getMenu().get(i).getDescription());
}
/** 运行结果
{
"menu": [{
"description": "Spaghetti",
"price_wrs": 7.99
}, {
"description": "steak",
"price_wrs": 12.99
}, {
"description": "salad",
"price_wrs": 5.99
}],
"name": "Future"
}
//解析结果
name:Future
0 :list: Spaghetti
1 :list: steak
2 :list: salad
*/
}
private void threeJson() {
List<RestaurantMenuItem> menuItems = new ArrayList<>();
menuItems.add(new RestaurantMenuItem("Spaghetti", 7.99f, "zhangfei"));
RestaurantWithMenu restaurantWithMenu = new RestaurantWithMenu("Future", menuItems);
Gson gson = new Gson();
String res = gson.toJson(restaurantWithMenu);
System.out.println("twojson: " + res);
/**
{
"menu": [{
"description": "Spaghetti",
"name": "zhangfei",
"price_wrs": 7.99
}],
"name": "Future"
}
*/
}
//生成简单的json字符串
private void oneJson() {
UserSimple userSimple = new UserSimple("Normal", "259175190@qq.com", 26, true);
Gson gson = new Gson();
String userJson = gson.toJson(userSimple);
System.out.println("wrs:" + userJson);
//解析字符串
userSimple = gson.fromJson(userJson, UserSimple.class);
System.out.println("age:" + userSimple.getAge() + "email:" + userSimple.getEmail());
/**运行结果:
{
"age": 26,
"email": "249175190@qq.com",
"isDeveloper": true,
"name": "Normal"
}
*/
}
// 心跳 说明:APP连接维持检测,每30秒上报一次。
private void heartbeatAction() {
HashMap<String,String> emplyees = new HashMap<>();
emplyees.put("Andress","arnold");
UserSimple userSimple = new UserSimple("Normal", "259175190@qq.com", 26, true,emplyees);
Gson gson = new Gson();
String userJson = gson.toJson(userSimple);
System.out.println("wrs:" + userJson);
/**
{
"age": 26,
"email": "259175190@qq.com",
"emplyees": {
"Andress": "arnold"
},
"isDeveloper": true,
"name": "Normal"
}
*/
}
}
最后
以上就是瘦瘦酒窝为你收集整理的GSON序列化json和解析json的全部内容,希望文章能够帮你解决GSON序列化json和解析json所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复