如何在 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/" } ] } |