我将以下JSON字符串作为请求参数输入到我的grails控制器中。
{
"loginName":"user1",
"timesheetList":
[
{
"periodBegin":"2014/10/12",
"periodEnd":"2014/10/18",
"timesheetRows":[
{
"task":"Cleaning",
"description":"cleaning description",
"paycode":"payCode1"
},
{
"task":"painting",
"activityDescription":"painting description",
"paycode":"payCode2"
}
]
}
],
"overallStatus":"SUCCESS"
}
如您所见,timesheetList可能包含多个元素。在这个(上面)的情况下,我们只有一个。所以,我希望它表现得像一个数组/列表。
然后我用下面的代码来解析它:
String saveJSON // This holds the above JSON string.
def jsonObject = grails.converters.JSON.parse(saveJSON) // No problem here. Returns a JSONObject. I checked the class type.
def jsonArray = jsonArray.timesheetList // No problem here. Returns a JSONArray. I checked the class type.
println "*** Size of jsonArray1: " + jsonArray1.size() // Returns size 1. It seemed fine as the above JSON string had only one timesheet in timesheetList
def timesheet1 = jsonArray[1] // This throws the JSONException, JSONArray[1] not found. I tried jsonArray.getJSONObject(1) and that throws the same exception.
基本上,我希望现在无缝迭代JSON字符串。有什么帮助吗?
答案 0 :(得分:0)
首先关闭简化代码,使用request.JSON
。然后request.JSON.list[ 0 ]
应该正常工作