จะทำให้วัตถุ JSON ซับซ้อนหรือแบนราบลงในโครงสร้าง Flat & Map-Like ใน Java ได้อย่างไร
เผยแพร่แล้ว: 2020-09-02
จะทำการดีซีเรียลไลซ์ JSON ที่ซ้อนกันเป็นโครงสร้างแบบเรียบเหมือนแผนที่ได้อย่างไร
สองสามวันก่อนฉันมีคำถามเกี่ยวกับวิธีการแผ่ JSON Object ซึ่งอาจง่ายของโครงสร้างที่ซับซ้อน?
JsonFlattener
เป็นยูทิลิตี้ maven ที่ทรงพลังมากสำหรับสิ่งเดียวกัน มาดูตัวอย่างกัน
คุณต้องนำเข้าด้านล่าง Maven Dependency ไปยังโปรเจ็กต์ของคุณ เพิ่มด้านล่างในไฟล์ 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 > |
หากคุณไม่เห็นไฟล์ pom.xml
ในพื้นที่ทำงาน Eclipse เนื่องจากคุณไม่มีโปรเจ็กต์ maven คุณสามารถแอบแฝงโปรเจ็กต์ maven ของโปรเจ็กต์ได้
side note
: เป็นการดีที่จะเห็นบทความ Crunchify ที่แสดงในหน้าผลการค้นหาของ Google

มาเริ่มกันเลย
ขั้นตอนที่ 1
- สร้างคลาส Java
CrunchifyJSONFlattenerTutorial
.java - หวังว่าคุณจะได้เพิ่มการพึ่งพา
json-flattener
ให้กับไฟล์pom.xml
ของคุณ
ขั้นตอนที่ 2
สร้างไฟล์ crunchify.txt
และวางไว้ใต้โฟลเดอร์ ~/Document
หากคุณใช้ Macbook ปรับเปลี่ยนเส้นทางตามความต้องการของคุณ
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 ใน java
ขั้นตอนที่ 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 ) ; } } |
และนั่นแหล่ะ เพียงเรียกใช้โปรแกรมแล้วคุณจะเห็นรูปแบบ Flatten
และ Unflatten
ของ JSONObject ของคุณ

เอาต์พุตคอนโซล 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/" } ] } |