json - 如何在 GJSON 中循环 slice

标签 json go

如何循环范围或正则表达式 #[1 - 12] 以在库 GJSON gjson library 中显示基于 #

像这样的例子,我在下面有图片 json :

enter image description here

我尝试像下面这样循环

tempPayments:="Envelope.Body.GetCustomReportResponse.GetCustomReportResult.ContractSummary.PaymentCalendarList.PaymentCalendar."
resultPriod1 := gjson.Get(jsonString,tempPayments + "#.Date")
for _, datePriod := range resultPriod1.Array() {
    println(datePriod[1].String())
}

如何在“#”中显示[1-10]范围内的结果?

最佳答案

Result.Array() 将当前结果转换为 Result 的 slice ([]Result)。因此,您可以按照您想要的范围切一片。例如resultPriod.Array()[0:12] 获取前 12 个元素。

以这个简化版本为例:

func main() {

    jsonString := `
{
    "PaymentCalendarList": {
        "PaymentCalendar": [
            {"ContractSubmitted": 10,
             "Date": "2018-01-01T01:01:01"},
            {"ContractSubmitted": 20,
             "Date": "2018-01-01T02:02:02"},
            {"ContractSubmitted": 30,
             "Date": "2018-01-01T03:03:03"}
            {"ContractSubmitted": 40,
             "Date": "2018-01-01T04:04:04"}
            {"ContractSubmitted": 50,
             "Date": "2018-01-01T05:05:05"}
        ]
    }
}`

    tempPayments := "PaymentCalendarList.PaymentCalendar."
    resultPeriod := gjson.Get(jsonString, tempPayments+"#.Date")

    # Take the first 3 results
    for _, datePeriod := range resultPeriod.Array()[0:3] {
        fmt.Println(datePeriod)
    }

}

Playground

关于json - 如何在 GJSON 中循环 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53405233/

相关文章:

az webapp list-runtimes 的 Azure REST API 等效项

java - Jackson readTree() 忽略 null 和空值

json - SwiftyJSON 解析数组对象的 JSON 错误

javascript - JSON 对象(如何最初不将整个对象加载到 DOM 上)

json - 如何在Go中解析firestore get()快照json

go - 软层 SDK SoftLayer_Exception_Public : Access Denied

docker - Golang docker library - 挂载主机目录卷

php - 通过 JSON、JQUERY、PHP 的图像预加载器

python - 在flask html页面中访问json数据

golang ssh 由 nc 替换 ssh ProxyCommand