当前位置:网站首页>[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)

[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)

2022-07-06 00:35:00 Programmer community

List of articles

  • One 、Groovy Object to json character string ( Use JsonBuilder convert )
  • Two 、 Use JsonOutput Convert an object of the specified type to json character string
  • 3、 ... and 、 take json String format output
  • Four 、 Complete code example

One 、Groovy Object to json character string ( Use JsonBuilder convert )


Statement Student class , Declare in it

2

2

2 Members , name and age ;

class Student { 
        def name    def age}

establish Student Object time , Assign values to these two members in the constructor

def student = new Student(name: "Tom", age: 18)

establish json generator JsonBuilder object , Constructor passed in Student object , Can finish json transformation , take Student The object is changed to json character string ;

// json  generator def jsonBuilder = new JsonBuilder(student)println jsonBuilder.toString()

Code example :

import groovy.json.JsonBuilderclass Student { 
        def name    def age}def student = new Student(name: "Tom", age: 18)// json  generator def jsonBuilder = new JsonBuilder(student)println jsonBuilder.toString()

Execution results :

{ 
    "age":18,"name":"Tom"}

Two 、 Use JsonOutput Convert an object of the specified type to json character string


JsonOutput Can be Map , URL , String , Number , Date , UUID , Boolean And other types of objects are converted to json character string ;
 Insert picture description here

take Student Object to json The code is as follows :

//  take  Student  Object to  jsondef json = JsonOutput.toJson(student)println json

Execution results :

{ 
    "age":18,"name":"Tom"}

3、 ... and 、 take json String format output


Use JsonOutput.prettyPrint(json) Can be json Format the output ,

The function prototype is as follows :

    /** * Pretty print a JSON payload. * * @param jsonPayload * @return a pretty representation of JSON payload. */    public static String prettyPrint(String jsonPayload) { 
        }

take {"age":18,"name":"Tom"} Use the format output above ,

//  Format output  json  data println JsonOutput.prettyPrint(json)

Output results :

{ 
        "age": 18,    "name": "Tom"}

Four 、 Complete code example


Complete code example :

import groovy.json.JsonBuilderimport groovy.json.JsonOutputclass Student { 
        def name    def age}def student = new Student(name: "Tom", age: 18)// json  generator def jsonBuilder = new JsonBuilder(student)println jsonBuilder.toString()//  take  Student  Object to  jsondef json = JsonOutput.toJson(student)println json//  Format output  json  data println JsonOutput.prettyPrint(json)

Execution results :

{ 
    "age":18,"name":"Tom"}{ 
    "age":18,"name":"Tom"}{ 
        "age": 18,    "name": "Tom"}

 Insert picture description here

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140223050531.html