Wie kann man komplexe JSON-Objekte in eine flache und kartenähnliche Struktur in Java abflachen oder aufheben?
Veröffentlicht: 2020-09-02
Wie deserialisiert man verschachteltes JSON in eine flache, kartenähnliche Struktur?
Vor ein paar Tagen hatte ich eine Frage zum Abflachen von JSON-Objekten, die eine einfache oder komplexe Struktur haben können?
JsonFlattener
ist ein sehr leistungsfähiges Maven-Dienstprogramm genau für dasselbe. Schauen wir uns ein Beispiel an.
Sie müssen die unten stehende Maven-Abhängigkeit in Ihr Projekt importieren. Fügen Sie unten der pom.xml-Datei Ihres Projekts hinzu.
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 > |
Wenn Sie die Datei pom.xml
nicht in Ihrem Eclipse-Arbeitsbereich sehen, da Sie kein Maven-Projekt haben, können Sie Ihr Projekt-Maven-Projekt einfach verdecken.
side note
: Es ist gut, den Crunchify-Artikel auf der Google-Suchergebnisseite zu sehen

Lass uns anfangen
Schritt 1
- Erstellen Sie die Java-Klasse
CrunchifyJSONFlattenerTutorial
.java - Ich hoffe, Sie haben Ihrer
pom.xml
-Datei die obigejson-flattener
Abhängigkeit hinzugefügt
Schritt 2
Erstellen Sie die Datei crunchify.txt
und legen Sie sie im Ordner ~/Document
ab, wenn Sie ein Macbook verwenden. Ändern Sie den Pfad nach Bedarf.
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" } ] } |
Wir werden die JSON-Datei crunchify.txt in Java lesen.
Schritt 3
Kopieren Sie den Java-Code in Ihr 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 ) ; } } |
Und das ist es. Führen Sie einfach das Programm aus und Sie sehen das Flatten
und Unflatten
-Format Ihres JSONObject.

Ausgabe der Eclipse-Konsole:
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/" } ] } |