Java에서 복잡한 JSON 객체를 평면 및 지도와 유사한 구조로 평면화 또는 평면화 해제하는 방법은 무엇입니까?
게시 됨: 2020-09-02
중첩된 JSON을 맵과 같은 평면 구조로 역직렬화하는 방법은 무엇입니까?
며칠 전에 구조가 복잡할 수 있는 JSON 개체를 평면화하는 방법에 대한 질문을 받았습니다.
JsonFlattener
는 정확히 동일한 매우 강력한 maven 유틸리티입니다. 예제를 살펴보겠습니다.
프로젝트에 Maven 종속성 아래로 가져와야 합니다. 프로젝트의 pom.xml 파일에 아래를 추가하십시오.
1 2 3 4 5 6 7 8 9 10 11 |
< dependency > < groupId > com . github . wnameless < / groupId > < artifactId > json - flattener < / artifactId > < version > 0.2.2 < / version > < / dependency > < dependency > < groupId > com . github . wnameless . json < / groupId > < artifactId > json - flattener < / artifactId > < version > 0.12.0 < / version > < / dependency > |
maven 프로젝트가 없기 때문에 Eclipse 작업 공간에 pom.xml
파일이 표시되지 않으면 프로젝트 maven 프로젝트를 간단히 숨길 수 있습니다.
side note
: Google 검색 결과 페이지에 Crunchify 기사가 실린 것을 보니 반갑습니다.

시작하자
1 단계
- 자바 클래스 생성
CrunchifyJSONFlattenerTutorial
.java -
pom.xml
파일에 위의json-flattener
종속성을 추가했길 바랍니다.
2 단계
crunchify.txt
파일을 생성하고 Macbook의 경우 ~/Document
폴더에 넣습니다. 필요에 따라 경로를 수정합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "Name" : "crunchify.com" , "Author" : "App Shah" , "Address" : "New York" , "Company Services" : [ { "Service" : "Site SEO Review" , "Link" : "https://pro.crunchify.com/#seo" } , { "Service" : "Full Website Design Service" , "Link" : "https://pro.crunchify.com/#full-design" } , { "Service" : "WordPress Optimization & Consultation" , "Link" : "https://pro.crunchify.com/#consultant" } ] } |
우리는 자바에서 crunchify.txt JSON 파일을 읽을 것입니다.
3단계
Eclipse에 자바 코드 복사:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package crunchify . com . tutorials ; import com . github . wnameless . json . flattener . JsonFlattener ; import com . github . wnameless . json . unflattener . JsonUnflattener ; import org . json . simple . JSONObject ; import org . json . simple . parser . JSONParser ; import java . io . FileReader ; import java . util . Map ; /** * @author Crunchify.com * How to Flatten or Unflatten Complex JSON objects into Flat & Map-Like Structure in Java? * */ public class CrunchifyJSONFlattenerTutorial { public static void main ( String [ ] args ) { JSONParser parser = new JSONParser ( ) ; try { /* crunchify.txt file content { "Name": "crunchify.com", "Author": "App Shah", "Address": "New York", "Company Services": [{ "Service": "Site SEO Review", "Link": "https://crunchify.com/services/site-seo-review-service/" }, { "Service": "Full Website Design Service", "Link": "https://crunchify.com/services/full-website-design-service/" }, { "Service": "WordPress Optimization & Consultation", "Link": "https://crunchify.com/services/wordpress-optimization-service/" }] } */ // Put above JSON content to crunchify.txt file and change path location Object obj = parser . parse ( new FileReader ( "/Users/appshah/Documents/crunchify.txt" ) ) ; JSONObject jsonObject = ( JSONObject ) obj ; // JsonFlattener: A Java utility used to FLATTEN nested JSON objects // The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. String flattenedJson = JsonFlattener . flatten ( jsonObject . toString ( ) ) ; log ( "\n========== Simple Flatten ========== \n" + flattenedJson ) ; Map < String , Object > flattenedJsonMap = JsonFlattener . flattenAsMap ( jsonObject . toString ( ) ) ; log ( "\n=====Flatten As Map=====\n" + flattenedJson ) ; // We are using Java8 forEach loop. More info: http://crunchify.me/1VIwm0l flattenedJsonMap . forEach ( ( k , v ) - > log ( k + " : " + v ) ) ; // Unflatten it back to original JSON String nestedJson = JsonUnflattener . unflatten ( flattenedJson ) ; System . out . println ( "\n===== Unflatten it back to original JSON ===== \n" + nestedJson ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } private static void log ( String flattenedJson ) { System . out . println ( flattenedJson ) ; } } |
그리고 그게 다야. 프로그램을 실행하면 JSONObject의 Flatten
및 Unflatten
형식이 표시됩니다.

Eclipse 콘솔 출력:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
===== Simple Flatten ===== { "Address" : "New York" , "Author" : "App Shah" , "Company Services[0].Service" : "Site SEO Review" , "Company Services[0].Link" : "https:\/\/crunchify.com\/services\/site-seo-review-service\/" , "Company Services[1].Service" : "Full Website Design Service" , "Company Services[1].Link" : "https:\/\/crunchify.com\/services\/full-website-design-service\/" , "Company Services[2].Service" : "WordPress Optimization & Consultation" , "Company Services[2].Link" : "https:\/\/crunchify.com\/services\/wordpress-optimization-service\/" , "Name" : "crunchify.com" } ===== Flatten As Map ===== { "Address" : "New York" , "Author" : "App Shah" , "Company Services[0].Service" : "Site SEO Review" , "Company Services[0].Link" : "https:\/\/crunchify.com\/services\/site-seo-review-service\/" , "Company Services[1].Service" : "Full Website Design Service" , "Company Services[1].Link" : "https:\/\/crunchify.com\/services\/full-website-design-service\/" , "Company Services[2].Service" : "WordPress Optimization & Consultation" , "Company Services[2].Link" : "https:\/\/crunchify.com\/services\/wordpress-optimization-service\/" , "Name" : "crunchify.com" } Address : New York Author : App Shah Company Services [ 0 ] . Service : Site SEO Review Company Services [ 0 ] . Link : https : //pro.crunchify.com/#seo Company Services [ 1 ] . Service : Full Website Design Service Company Services [ 1 ] . Link : https : //pro.crunchify.com/#full-design Company Services [ 2 ] . Service : WordPress Optimization & Consultation Company Services [ 2 ] . Link : https : //pro.crunchify.com/#consultant Name : crunchify . com ===== Unflatten it back to original JSON ===== { "Name" : "crunchify.com" , "Author" : "App Shah" , "Address" : "New York" , "Company Services" : [ { "Service" : "Site SEO Review" , "Link" : "https://crunchify.com/services/site-seo-review-service/" } , { "Service" : "Full Website Design Service" , "Link" : "https://crunchify.com/services/full-website-design-service/" } , { "Service" : "WordPress Optimization & Consultation" , "Link" : "https://crunchify.com/services/wordpress-optimization-service/" } ] } |