2021年2月7日星期日

Retrieve dynamic objects within JSON object and put to an array Groovy

I'm new to Groovy and couldn't find any resources related to this. I'm trying to extract data from a "dates" object and put it in an array of fixed objects within it. The reason for it is that the objects within dates will change and I'd like to put into fixed for easy manipulation:

Original:

{  "dates": {      "2021-02-08": "8",      "2021-02-09": "8",      "2021-02-10": "8"  }}  

Output wanted:

{  "dates": [      {      "date": "2021-02-08",      "value": "8"      },      {      "date": "2021-02-09",      "value": "8"      },      {      "date": "2021-02-10",      "value": "8"      },  ]}  

I'm trying to use the current codes:

arrayList = []  arrayList.add(content[0].dates)  

It returns an [[2021-02-08:8, 2021-02-09:8, 2021-02-10:8]], but I cannot extract it as an array.

Any thoughts?

https://stackoverflow.com/questions/66095084/retrieve-dynamic-objects-within-json-object-and-put-to-an-array-groovy February 08, 2021 at 10:06AM

没有评论:

发表评论