//Parsing Text
import groovy.json.JsonSlurper
class Example {
static void main(String[] args) {
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText('{ "name": "John", "ID" : "1"}')
println(object.name);
println(object.ID);
}
}
//Parsing List of Integers
import groovy.json.JsonSlurper
class Example {
static void main(String[] args) {
def jsonSlurper = new JsonSlurper()
Object lst = jsonSlurper.parseText('{ "List": [2, 3, 4, 5] }')
lst.each { println it }
}
}
//parsing list of primitive data types
import groovy.json.JsonSlurper
class Example {
static void main(String[] args) {
def jsonSlurper = new JsonSlurper()
def obj = jsonSlurper.parseText ''' {"Integer": 12, "fraction": 12.55, "double": 12e13}'''
println(obj.Integer);
println(obj.fraction);
println(obj.double);
}
}
//ToJson
import groovy.json.JsonOutput
class Example {
static void main(String[] args) {
def output = JsonOutput.toJson([name: 'John', ID: 1])
println(output);
}
}
import groovy.json.JsonOutput
class Example {
static void main(String[] args) {
def output = JsonOutput.toJson([ new Student(name: 'John',ID:1),
new Student(name: 'Mark',ID:2)])
println(output);
}
}
class Student {
String name
int ID;
}
No comments:
Post a Comment