'ListtoJson'에 해당되는 글 1건

  1. 2021.03.17 List를 Json으로 변환 예제
Java2021. 3. 17. 14:16

 

 

 

자바 ArrayList를 Json 객체로 변경하는 예제..

 

 

import net.sf.json.JSONArray;
import com.google.gson.Gson; 

// 라이브러리가 없으면 메이븐이나 jar파일을 직접 다운로드해서 import..

List resultList = new ArrayList<String>();
resultList.add("a");
resultList.add("b");
resultList.add("c");

// 방법 1
//JSONArray 객체 사용
JSONArray result = JSONArray.fromObject(resultList); //HashMap 등의 복잡한 자료형으로 덮어도 가능
System.out.println("제이슨 결과 = " + mapResult.toString());

// 방법 2
// Gson 사용
String json = new Gson().toJson(resultList);
System.out.println("제이슨 결과2 = " + json;

 

 

* GSon 라이브러리 출처 및 참고

https://github.com/google/gson

 

Posted by 사슴영혼'-'