Read all element of JSon httpPOST

(vincent jérémy) #1


I work on a project of communication between an application android and a Smartapp.
My application sends through an Https request a JSon of type:

“ROOM0”: {
“Name”: “kitchen”
“Device1”: "thermos3"
“Device0”: “Thermos4”
“Room1”: {
“Name”: “livingroom”
“Device1”: "thermos2"
“Device0”: “thermos1”

I can recover the JSon Room0 and Room1 sparse, but I can not retrieve each element of my JSon afterwards.

My code:

mappings {
path("/allheat") { action: [ GET: “heatDevises” ]}
path("/changeheat") { action: [ POST: “sendHeatCommands” ]}
path("/listRoom") { action: [ POST: “listRoomGet” ]}


def listRoomGet() {
debug(“listRoomGet called”)

def Room01  = request.JSON?.Room0
def Room02  = request.JSON?.Room1

def results = [ :]
results << [name1 :  Room01]  //{"Name": "kitchen","Device1": "thermos3","Device0": "Thermos4"}
results << [name2 :  Room02]  //{"Name": "living","Device1": "thermos2","Device0": "Thermos1"}

def nameR  = Room01.JSON?.Name
results << [name :  nameR]

render contentType: "text/json", data: new JsonBuilder(results).toPrettyString()


How can I recover my items separately?

(vincent jérémy) #2

Ok I’m Going to Sleep !!!

I find the solution …

def nameR = Room01.Name

I’m null, I know ^^

(Michael) #3

Use JsonSlurper(). You can search Google for examples. Here is another thread with details too: