我有以下json:
onBindViewHolder
我想在def desiredJson = '{"count": 4, "max": "12", "min": 0, "details": [{"goBus": {"first": 12800, "second": 11900, "third": 12800},"goAir": {"first": 12800, "second": 11900, "third": 12800}, "gotTrain": {"first": 12800, "second": 11900},"sell": true, "darn": 2,"rate": [{ "busRate": 11900, "flag": false, "percent": 0}],}],}'
之后添加"total": "1000"
键值对。
到目前为止,我已尝试过以下内容:
"sell": true
但是它在任何地方附加了这对。像这样:
def json = new JsonSlurper().parseText(desiredJson)
json.put('total', '1000')
log.info JsonOutput.toJson(json)
我们可以在任何特定位置添加它吗?我已经解决了这个问题 - Add JSON object to Another JSON Object at particular place。但我无法从中得到任何线索。
此外,如果这个desiredJson有这样的重复对象:
{
"count": 4,
"details": [{
"darn": 2,
"goAir": {
"first": 12800,
"second": 11900,
"third": 12800
},
"goBus": {
"first": 12800,
"second": 11900,
"third": 12800
},
"gotTrain": {
"first": 12800,
"second": 11900
},
"rate": [{
"busRate": 11900,
"flag": false,
"percent": 0
}],
"sell": true
}],
"max": "12",
"min": 0,
"total": "1000"
}
应添加总密钥的值并将其递增。
答案 0 :(得分:3)
答案 1 :(得分:1)
首先 - 如果要在文档中重复JSON对象,则必须将其放入数组中。在您的情况下,有效的JSON文档将如下所示:
[
{
"count": 4,
"details": [
{
"darn": 2,
"goAir": {
"first": 12800,
"second": 11900,
"third": 12800
},
"goBus": {
"first": 12800,
"second": 11900,
"third": 12800
},
"gotTrain": {
"first": 12800,
"second": 11900
},
"rate": [
{
"busRate": 11900,
"flag": false,
"percent": 0
}
],
"sell": true
}
],
"max": "12",
"min": 0
},
{
"count": 4,
"details": [
{
"darn": 2,
"goAir": {
"first": 12800,
"second": 11900,
"third": 12800
},
"goBus": {
"first": 12800,
"second": 11900,
"third": 12800
},
"gotTrain": {
"first": 12800,
"second": 11900
},
"rate": [
{
"busRate": 11900,
"flag": false,
"percent": 0
}
],
"sell": true
}
],
"max": "12",
"min": 0
},
{
"count": 4,
"details": [
{
"darn": 2,
"goAir": {
"first": 12800,
"second": 11900,
"third": 12800
},
"goBus": {
"first": 12800,
"second": 11900,
"third": 12800
},
"gotTrain": {
"first": 12800,
"second": 11900
},
"rate": [
{
"busRate": 11900,
"flag": false,
"percent": 0
}
],
"sell": true
}
],
"max": "12",
"min": 0
}
]
现在,如果你想在路径total
中添加$.details.total
字段数组中的每个对象,那么你可以这样做:
def json = new JsonSlurper().parseText(desiredJson)
int total = 1000
json.details.each { it.get(0).put('total', total++) }
println JsonOutput.prettyPrint(JsonOutput.toJson(json))
如果您想将total
字段添加到路径$.details.rate.total
,请执行以下操作:
json.details.rate.each { it.flatten().get(0).put('total', total++) }
代替。
[
{
"count": 4,
"details": [
{
"darn": 2,
"goAir": {
"first": 12800,
"second": 11900,
"third": 12800
},
"goBus": {
"first": 12800,
"second": 11900,
"third": 12800
},
"gotTrain": {
"first": 12800,
"second": 11900
},
"rate": [
{
"busRate": 11900,
"flag": false,
"percent": 0
}
],
"sell": true,
"total": 1000
}
],
"max": "12",
"min": 0
},
{
"count": 4,
"details": [
{
"darn": 2,
"goAir": {
"first": 12800,
"second": 11900,
"third": 12800
},
"goBus": {
"first": 12800,
"second": 11900,
"third": 12800
},
"gotTrain": {
"first": 12800,
"second": 11900
},
"rate": [
{
"busRate": 11900,
"flag": false,
"percent": 0
}
],
"sell": true,
"total": 1001
}
],
"max": "12",
"min": 0
},
{
"count": 4,
"details": [
{
"darn": 2,
"goAir": {
"first": 12800,
"second": 11900,
"third": 12800
},
"goBus": {
"first": 12800,
"second": 11900,
"third": 12800
},
"gotTrain": {
"first": 12800,
"second": 11900
},
"rate": [
{
"busRate": 11900,
"flag": false,
"percent": 0
}
],
"sell": true,
"total": 1002
}
],
"max": "12",
"min": 0
}
]
请注意,它会在total
键之后添加sell
,但从JSON文档验证角度来看,无论是在sell
字段之前还是之后都无关紧要。这个文档何时被消费者解析甚至不应该。