只使用已解组的 JSON 数据的值

huangapple go评论81阅读模式
英文:

Using only the values of JSON data that is unmarshalled

问题

我正在寻找一种方法,将一组ID添加到一个切片中。然而,如果我不自定义解组数据,我无法获取到ID的值,而只是一个JSON对象数组。如果我自定义解组数据,我会被告知无法将这个类似切片的数据追加到切片中,因为它具有我用于解组的结构体的自定义类型。

我希望只发出一次请求,从REST API中获取"data"和"meta"信息,然后将数据解组成以下格式:

{Data:[{Ids:39647} {Ids:39648} {Ids:39649} {Ids:39650} {Ids:39651} {Ids:39652} {Ids:39653} {Ids:39654} {Ids:39655} {Ids:39656} {Ids:39657} {Ids:39658} {Ids:39659} {Ids:39660} {Ids:39661} {Ids:39662} {Ids:39663} {Ids:39664} {Ids:39665} {Ids:39666} {Ids:39667} {Ids:39668} {Ids:39669} {Ids:39670} {Ids:39671} {Ids:39672} {Ids:39673}] Meta:{Metadata:{CurrentPage:3 TotalPages:656}}}

我想要将Ids获取为一个切片,如下所示:

[39647 39649 39650 ...]

最好不要再进行编组和解组,但是现实有时不容许。这个切片需要是[]int类型,以便我可以将其追加到另一个切片中,并使用所有标准库切片的功能。

编辑:有人要求我在问题中添加基本的JSON结构。

{
  "data": [
    {
      "id": 38926
    },
    {
      "id": 38927
    },
    //...以此类推。
  ],
  "meta": {
    "pagination": {
      "total": 163795,
      "current_page": 3,
      "total_pages": 81898
    }
  }
}
英文:

I'm looking to take a list of ids and add them to a slice. However if I don't custom unmarshall the data, I cannot get to the values of the ids and it is just an array of JSON objects. And if I do custom unmarshall the data I want, I am told that I cannot append that slice-like array of data because it has a custom type from the structs I used to unmarshall.

I would prefer to only make the request once to get both the "data" and "meta" information from a rest API, I then unmarshall that data into the following:


{Data:[{Ids:39647} {Ids:39648} {Ids:39649} {Ids:39650} {Ids:39651} {Ids:39652} {Ids:39653} {Ids:39654} {Ids:39655} {Ids:39656} {Ids:39657} {Ids:39658} {Ids:39659} {Ids:39660} {Ids:39661} {Ids:39662} {Ids:39663} {Ids:39664} {Ids:39665} {Ids:39666} {Ids:39667} {Ids:39668} {Ids:39669} {Ids:39670} {Ids:39671} {Ids:39672} {Ids:39673}] Meta:{Metadata:{CurrentPage:3 TotalPages:656}}}

I would like to get the Ids in a slice like so:

[39647 39649 39650 ...]

Preferably without having to marshall and then unmarshall again, but beggars can't be choosers. This would then need to be of slice type []int so that I can append it to another slice and use all standard library slice things with it.

Edit: It was requested I add the basic JSON Structure to the question.

  "data": [
    {
      "id": 38926
    },
    {
      "id": 38927
    },
    //... and so on.
  ],
  "meta": {
    "pagination": {
      "total": 163795,
      "current_page": 3,
      "total_pages": 81898
    }
  }
}

答案1

得分: 1

如果你只想获取int类型的"id"字段,而不想解析整个JSON,你可以使用一个JSON解析库来实现,比如https://github.com/tidwall/gjson。

package main

import "github.com/tidwall/gjson"

json := `{
	"data":[
	  {
		 "id":38926
	  },
	  {
		 "id":38927
	  }
	],
	"meta":{
	  "pagination":{
		 "total":163795,
		 "current_page":3,
		 "total_pages":81898
	   }
	 }
}`

func main() {
	value := gjson.Get(json, "data.#.id")
	println(value.Array()) // etc
}
英文:

If you just want to get a straight slice of int-"id",without unmarshalling the whole JSON, you could use a JSON parser library to do that, ex-https://github.com/tidwall/gjson.

package main

import "github.com/tidwall/gjson"

json := `{
 "data":[
   {
	  "id":38926
   },
   {
	  "id":38927
   }
 ],
 "meta":{
   "pagination":{
	  "total":163795,
	  "current_page":3,
	  "total_pages":81898
    }
  }
}`

func main() {
	value := gjson.Get(json, "data.#.id")
	println(value.Array()) // etc
}

huangapple
  • 本文由 发表于 2021年12月29日 00:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70509998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定