Skip to content

Instantly share code, notes, and snippets.

@yhishi
Last active September 25, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhishi/e028e30e5ef59cf748de7183b5a820a6 to your computer and use it in GitHub Desktop.
Save yhishi/e028e30e5ef59cf748de7183b5a820a6 to your computer and use it in GitHub Desktop.
gson.toJson
import com.google.gson.Gson;
/**
* 検索機能などでDBから値を取得し、クライアント側にリストを返すとき、
* Javaオブジェクトでは返せないため、JSON形式に変換する
* ※dblistはDBから取得した<ArrayList<String>>
*/
// Gsonオブジェクトを作成
Gson gson = new Gson();
// クライアントにDBからのデータリストを返す
PrintWriter out = response.getWriter();
// (Javaオブジェクト⇒JSON)
out.println(gson.toJson(dblist));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment