Java JsonGenerator – JSON処理APIとプリティプリントJSON出力を有効にする方法(Gson + Jackson)
公開: 2014-11-22
これは非常に興味深いチュートリアルになります。 エンタープライズJavaアプリケーションでは、大量のJSONデータを処理する必要がある場合があります。 ファイルへの書き込み、ファイルからの読み取り、素敵なPrettyFormatでの適切なログ記録などが行われる場合があります。
Pretty-JavaでのJSONの印刷について疑問に思ったことはありますか? このチュートリアルでは、 javax.json
パッケージとJsonGenerator
APIを使用して、JSONObjectをファイルに書き込みます。 また、 com.google.gson.Gson
を使用してJSON出力を美化します。
実行する手順は次のとおりです。
- クラス
CrunchifyJsonGeneratorPrettyJSON.java
作成します - JsonGeneratorを使用してJavaでJSONObjectを作成し、/ Users / appshah / Desktop /crunchifyJson.txtに保存します。
- ファイルから同じJSONを読み取る
- Eclipseコンソールで単純なJSONを印刷する
crunchifyPrettyJSONUtility()
ユーティリティを使用して単純なJSONをPrettyJSONに変換する–JSON文字列をPrettyPrintに変換する(Java、Gson)- 同じPrettyJSONをコンソールに印刷する
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
package crunchify . com . tutorials ; import java . io . FileNotFoundException ; import java . io . FileReader ; import java . io . FileWriter ; import java . io . IOException ; import javax . json . Json ; import javax . json . stream . JsonGenerator ; import org . json . simple . parser . JSONParser ; import org . json . simple . parser . ParseException ; import com . google . gson . Gson ; import com . google . gson . GsonBuilder ; import com . google . gson . JsonObject ; import com . google . gson . JsonParser ; /** * @author Crunchify.com * JsonGenerator and Pretty */ public class CrunchifyJsonGeneratorPrettyJSON { public static void main ( String [ ] args ) { FileWriter writer = null ; JSONParser parser = new JSONParser ( ) ; Object simpleObj = null ; try { writer = new FileWriter ( "/Users/appshah/Desktop/crunchifyJson.txt" ) ; // Modify path as per your need } catch ( IOException e ) { e . printStackTrace ( ) ; } // JsonGenerator to create JSONObject and store it to file location mentioned above JsonGenerator generator = Json . createGenerator ( writer ) ; generator . writeStartObject ( ) . writeStartArray ( "company" ) . writeStartObject ( ) . write ( "name" , "Crunchify" ) . write ( "managedBy" , "App Shah" ) . write ( "address" , "NYC, US" ) . writeStartObject ( "support" ) . write ( "type" , "wordpress" ) . write ( "status" , "active" ) . writeEnd ( ) . write ( "support_for" , "WordPress Plugins" ) . write ( "id" , "24534-4324-6f3453-4234-w234234" ) . write ( "team" , "3" ) . writeEnd ( ) . writeStartObject ( ) . write ( "name" , "Google" ) . write ( "managedBy" , "Larry Page,Sergey Brin" ) . write ( "address" , "Mountain View, US" ) . writeStartObject ( "support" ) . writeStartArray ( "products" ) . write ( "Gmail" ) . write ( "YouTube" ) . write ( "Drive" ) . write ( "+ Lot More" ) . writeEnd ( ) . write ( "status" , "active" ) . writeEnd ( ) . write ( "support_for" , "gmail, drive, YouTube and lot more" ) . write ( "id" , "3fwevwere-vwerfwevw-erw-vwe-efwfw" ) . write ( "team" , "46000" ) . writeEnd ( ) . writeEnd ( ) . writeEnd ( ) ; generator . close ( ) ; try { simpleObj = parser . parse ( new FileReader ( "/Users/appshah/Desktop/crunchifyJson.txt" ) ) ; } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( ParseException e ) { e . printStackTrace ( ) ; } System . out . println ( "Simple JSON Result:\n" + simpleObj . toString ( ) ) ; String prettyJson = crunchifyPrettyJSONUtility ( simpleObj . toString ( ) ) ; System . out . println ( "\nPretty JSON Result:\n" + prettyJson ) ; } // Prettify JSON Utility public static String crunchifyPrettyJSONUtility ( String simpleJSON ) { JsonParser crunhifyParser = new JsonParser ( ) ; JsonObject json = crunhifyParser . parse ( simpleJSON ) . getAsJsonObject ( ) ; Gson prettyGson = new GsonBuilder ( ) . setPrettyPrinting ( ) . create ( ) ; String prettyJson = prettyGson . toJson ( json ) ; return prettyJson ; } } |
インターフェイスJsonGenerator
は、JSONデータをストリーミング方式で出力ソースに書き込みます。 クラスJsonには、文字ストリームまたは出力ストリームのジェネレーターを作成するためのメソッドが含まれています。

Gson
は、JavaオブジェクトをJSON表現に変換するために使用できるJavaライブラリです。 JSON文字列を同等のJavaオブジェクトに変換するためにも使用できます。 つまり、json処理チュートリアル用のjava api、json処理maven用のjava api、json処理例用のjavaapiなどです。
それを機能させるには、2つ以下のMaven依存関係が必要です。
1 2 3 4 5 |
< dependency > < groupId > org . glassfish < / groupId > < artifactId > javax . json < / artifactId > < version > 1.0.4 < / version > < / dependency > |
1 2 3 4 5 |
< dependency > < groupId > com . google . code . gson < / groupId > < artifactId > gson < / artifactId > < version > 2.3 < / version > < / dependency > |
結果は次のとおりです。
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 |
Simple JSON Result : { "company" : [ { "id" : "24534-4324-6f3453-4234-w234234" , "support" : { "status" : "active" , "type" : "wordpress" } , "address" : "NYC, US" , "name" : "Crunchify" , "managedBy" : "App Shah" , "team" : "3" , "support_for" : "WordPress Plugins" } , { "id" : "3fwevwere-vwerfwevw-erw-vwe-efwfw" , "support" : { "status" : "active" , "products" : [ "Gmail" , "YouTube" , "Drive" , "+ Lot More" ] } , "address" : "Mountain View, US" , "name" : "Google" , "managedBy" : "Larry Page,Sergey Brin" , "team" : "46000" , "support_for" : "gmail, drive, YouTube and lot more" } ] } Pretty JSON Result : { "company" : [ { "id" : "24534-4324-6f3453-4234-w234234" , "support" : { "status" : "active" , "type" : "wordpress" } , "address" : "NYC, US" , "name" : "Crunchify" , "managedBy" : "App Shah" , "team" : "3" , "support_for" : "WordPress Plugins" } , { "id" : "3fwevwere-vwerfwevw-erw-vwe-efwfw" , "support" : { "status" : "active" , "products" : [ "Gmail" , "YouTube" , "Drive" , "+ Lot More" ] } , "address" : "Mountain View, US" , "name" : "Google" , "managedBy" : "Larry Page,Sergey Brin" , "team" : "46000" , "support_for" : "gmail, drive, YouTube and lot more" } ] } |