如何使用groovy将json字段添加到所需的json和特定的地方?

时间:2018-01-08 10:29:52

标签: json groovy

我有以下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"
}

应添加总密钥的值并将其递增。

2 个答案:

答案 0 :(得分:3)

不,你不能(确定你可以,但为什么这么麻烦)。原因如下:

  

对象:名称 - 值对的无序集合

Source

因此,如果某个上游需要按顺序排列,那么它就会被破坏。

答案 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字段之前还是之后都无关紧要。这个文档何时被消费者解析甚至不应该。