package com.ryan.practice.rest;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import org.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JSONObjectPractice {
public JSONObject printJSONObject() {
JSONObject jsonObject = new JSONObject();
try {
Field changeMap = jsonObject.getClass().getDeclaredField("map");
changeMap.setAccessible(true);
changeMap.set(jsonObject, new LinkedHashMap<>());
changeMap.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {
log.info(e.getMessage());
}
jsonObject.put("one", "hi");
jsonObject.put("two", "there");
jsonObject.put("three", "whats");
jsonObject.put("four", "up");
return jsonObject;
}
}
/* Reference */
https://towardsdatascience.com/create-an-ordered-jsonobject-in-java-fb9629247d76
Create an Ordered JSONObject in Java
Why is it so complicated to create an ordered JSONObject?
towardsdatascience.com
https://junhyunny.blogspot.com/2019/03/field-getdeclaredfields-setaccessible.html
[Field 클래스] getDeclaredFields / setAccessible 메소드
초급 개발자가 공부한 내용을 정리하는 블로그입니다.
junhyunny.blogspot.com
https://junhyunny.blogspot.com/2019/02/java-reflection.html
Java Reflection
초급 개발자가 공부한 내용을 정리하는 블로그입니다.
junhyunny.blogspot.com
'Java & Spring' 카테고리의 다른 글
| PKIX path building failed 에러 해결 (0) | 2023.03.29 |
|---|---|
| gradle build시 다른 gradle project 참조하는 방법 (0) | 2023.03.09 |
| IntelliJ 단축키 (0) | 2023.03.09 |
| 코딩테스트 대비 Java 기본문법 레퍼런스 (0) | 2023.01.25 |
| Logback 설정 정리 (0) | 2023.01.11 |
댓글