简体   繁体   中英

JSON not print as Pretty Print using GSON in Scala

My code is as below :

 var json_val = (Json(DefaultFormats).write(map))

The above json value is like as below :

{"Tatyana Nader":[{"source":"Marseille Rail Station (XRF)","amount":"5000.0","points":"500","date_time":"Sun Dec 06 07:36:09 IST 2015","class":"First","destination":"Batna Arpt (BLJ)"}]}

I want to print this json data as beautify JSON / Preety Json.

So I have used gson for that as below :

var gson:Gson = null
gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create()
var jp: JsonParser = null
var je: JsonElement = null
je = jp.parse(json_val)
var json: String = gson.toJson(json_val)
var prettyJsonString: String = gson.toJson(je)

I have tried lot's of time but it's returning Null Pointer Exception every time. Please advice on it.

Thank you in advance.

if you are using Play Json

import play.api.libs.json.Json

val jsonString = """{"Tatyana Nader":[{"source":"Marseille Rail Station (XRF)","amount":"5000.0","points":"500","date_time":"Sun Dec 06 07:36:09 IST 2015","class":"First","destination":"Batna Arpt (BLJ)"}]}"""

Json.prettyPrint(Json.parse(jsonString)).toString

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM