如何在 Java 中將復雜的 JSON 對象展平或不展平為平面和類似地圖的結構?
已發表: 2020-09-02
如何將嵌套的 JSON 反序列化為扁平的、類似 Map 的結構?
幾天前,我收到了一個關於如何展平結構可能很簡單或複雜的 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 > |
如果您沒有在 Eclipse 工作區中看到pom.xml
文件,因為您沒有 maven 項目,那麼您可以簡單地隱藏您的項目 maven 項目。
side note
:很高興看到谷歌搜索結果頁面中的 Crunchify 文章

讓我們開始吧
第1步
- 創建 java 類
CrunchifyJSONFlattenerTutorial
.java - 希望您已將上述
json-flattener
依賴項添加到您的pom.xml
文件中
第2步
如果您在 Macbook 上,請創建文件crunchify.txt
並將其放在~/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" } ] } |
我們將在 java 中讀取 crunchify.txt JSON 文件。
第三步
將 java 代碼複製到 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/" } ] } |