程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 應用GSON庫將Java中的map鍵值對應構造對象轉換為JSON

應用GSON庫將Java中的map鍵值對應構造對象轉換為JSON

編輯:關於JAVA

應用GSON庫將Java中的map鍵值對應構造對象轉換為JSON。本站提示廣大學習愛好者:(應用GSON庫將Java中的map鍵值對應構造對象轉換為JSON)文章只能為提供參考,不一定能成為您想要的結果。以下是應用GSON庫將Java中的map鍵值對應構造對象轉換為JSON正文


Map的存儲構造式Key/Value情勢,Key 和 Value可所以通俗類型,也能夠是本身寫的JavaBean(本文),還可所以帶有泛型的List.

(GSON的GitHub項目頁:https://github.com/谷歌/gson)

JavaBean

本例中您要重點看若何將Json轉回為通俗JavaBean對象時TypeToken的界說.

實體類:

public class Point { 
  private int x; 
  private int y; 
 
  public Point(int x, int y) { 
    this.x = x; 
    this.y = y; 
  } 
 
  public int getX() { 
    return x; 
  } 
 
  public void setX(int x) { 
    this.x = x; 
  } 
 
  public int getY() { 
    return y; 
  } 
 
  public void setY(int y) { 
    this.y = y; 
  } 
 
  @Override 
  public String toString() { 
    return "Point [x=" + x + ", y=" + y + "]"; 
  } 
 
} 

測試類:

import java.util.LinkedHashMap; 
import java.util.Map; 
 
import com.谷歌.gson.Gson; 
import com.谷歌.gson.GsonBuilder; 
import com.谷歌.gson.reflect.TypeToken; 
 
public class GsonTest3 { 
 
  public static void main(String[] args) { 
    Gson gson = new GsonBuilder().enableComplexMapKeySerialization() 
        .create(); 
 
    Map<Point, String> map1 = new LinkedHashMap<Point, String>();// 應用LinkedHashMap將成果按先輩先出次序分列 
    map1.put(new Point(5, 6), "a"); 
    map1.put(new Point(8, 8), "b"); 
    String s = gson.toJson(map1); 
    System.out.println(s);// 成果:[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]] 
 
    Map<Point, String> retMap = gson.fromJson(s, 
        new TypeToken<Map<Point, String>>() { 
        }.getType()); 
    for (Point p : retMap.keySet()) { 
      System.out.println("key:" + p + " values:" + retMap.get(p)); 
    } 
    System.out.println(retMap); 
 
    System.out.println("----------------------------------"); 
    Map<String, Point> map2 = new LinkedHashMap<String, Point>(); 
    map2.put("a", new Point(3, 4)); 
    map2.put("b", new Point(5, 6)); 
    String s2 = gson.toJson(map2); 
    System.out.println(s2); 
 
    Map<String, Point> retMap2 = gson.fromJson(s2, 
        new TypeToken<Map<String, Point>>() { 
        }.getType()); 
    for (String key : retMap2.keySet()) { 
      System.out.println("key:" + key + " values:" + retMap2.get(key)); 
    } 
 
  } 
} 

成果:

[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]] 
key:Point [x=5, y=6] values:a 
key:Point [x=8, y=8] values:b 
{Point [x=5, y=6]=a, Point [x=8, y=8]=b} 
---------------------------------- 
{"a":{"x":3,"y":4},"b":{"x":5,"y":6}} 
key:a values:Point [x=3, y=4] 
key:b values:Point [x=5, y=6] 

泛型List

實體類:

import java.util.Date; 
 
public class Student { 
  private int id; 
  private String name; 
  private Date birthDay; 
 
  public int getId() { 
    return id; 
  } 
 
  public void setId(int id) { 
    this.id = id; 
  } 
 
  public String getName() { 
    return name; 
  } 
 
  public void setName(String name) { 
    this.name = name; 
  } 
 
  public Date getBirthDay() { 
    return birthDay; 
  } 
 
  public void setBirthDay(Date birthDay) { 
    this.birthDay = birthDay; 
  } 
 
  @Override 
  public String toString() { 
    return "Student [birthDay=" + birthDay + ", id=" + id + ", name=" 
        + name + "]"; 
  } 
 
} 


public class Teacher { 
  private int id; 
 
  private String name; 
 
  private String title; 
 
  public int getId() { 
    return id; 
  } 
 
  public void setId(int id) { 
    this.id = id; 
  } 
 
  public String getName() { 
    return name; 
  } 
 
  public void setName(String name) { 
    this.name = name; 
  } 
 
  public String getTitle() { 
    return title; 
  } 
 
  public void setTitle(String title) { 
    this.title = title; 
  } 
 
  @Override 
  public String toString() { 
    return "Teacher [id=" + id + ", name=" + name + ", title=" + title 
        + "]"; 
  } 
 
} 

測試類:

package com.tgb.lk.demo.gson.test4; 
 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.LinkedHashMap; 
import java.util.List; 
import java.util.Map; 
 
import com.谷歌.gson.Gson; 
import com.谷歌.gson.reflect.TypeToken; 
 
public class GsonTest4 { 
  public static void main(String[] args) { 
    Student student1 = new Student(); 
    student1.setId(1); 
    student1.setName("李坤"); 
    student1.setBirthDay(new Date()); 
 
    Student student2 = new Student(); 
    student2.setId(2); 
    student2.setName("曹貴生"); 
    student2.setBirthDay(new Date()); 
 
    Student student3 = new Student(); 
    student3.setId(3); 
    student3.setName("柳波"); 
    student3.setBirthDay(new Date()); 
 
    List<Student> stulist = new ArrayList<Student>(); 
    stulist.add(student1); 
    stulist.add(student2); 
    stulist.add(student3); 
 
    Teacher teacher1 = new Teacher(); 
    teacher1.setId(1); 
    teacher1.setName("米先生"); 
    teacher1.setTitle("傳授"); 
 
    Teacher teacher2 = new Teacher(); 
    teacher2.setId(2); 
    teacher2.setName("丁先生"); 
    teacher2.setTitle("講師"); 
    List<Teacher> teacherList = new ArrayList<Teacher>(); 
    teacherList.add(teacher1); 
    teacherList.add(teacher2); 
 
    Map<String, Object> map = new LinkedHashMap<String, Object>(); 
    map.put("students", stulist); 
    map.put("teachers", teacherList); 
 
    Gson gson = new Gson(); 
    String s = gson.toJson(map); 
    System.out.println(s); 
 
    System.out.println("----------------------------------"); 
 
    Map<String, Object> retMap = gson.fromJson(s, 
        new TypeToken<Map<String, List<Object>>>() { 
        }.getType()); 
 
    for (String key : retMap.keySet()) { 
      System.out.println("key:" + key + " values:" + retMap.get(key)); 
      if (key.equals("students")) { 
        List<Student> stuList = (List<Student>) retMap.get(key); 
        System.out.println(stuList); 
      } else if (key.equals("teachers")) { 
        List<Teacher> tchrList = (List<Teacher>) retMap.get(key); 
        System.out.println(tchrList); 
      } 
    } 
 
  } 
} 

輸入成果:

{"students":[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:48:19 PM"},{"id":2,"name":"曹貴生","birthDay":"Jun 22, 2012 9:48:19 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 9:48:19 PM"}],"teachers":[{"id":1,"name":"米先生","title":"傳授"},{"id":2,"name":"丁先生","title":"講師"}]} 
---------------------------------- 
key:students values:[{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹貴生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}] 
[{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹貴生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}] 
key:teachers values:[{id=1.0, name=米先生, title=傳授}, {id=2.0, name=丁先生, title=講師}] 
[{id=1.0, name=米先生, title=傳授}, {id=2.0, name=丁先生, title=講師}] 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved