How to parse partial objects inside Json

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

How to parse partial objects inside Json

问题

我有以下的Json结构,我想解析对象内部的key字段。有没有可能在不映射完整结构的情况下做到这一点?

  1. {
  2. "Records": [
  3. {
  4. "eventVersion": "2.0",
  5. "s3": {
  6. "s3SchemaVersion": "1.0",
  7. "configurationId": "my-event",
  8. "bucket": {
  9. "name": "super-files",
  10. "ownerIdentity": {
  11. "principalId": "41123123"
  12. },
  13. "arn": "arn:aws:s3:::myqueue"
  14. },
  15. "object": {
  16. "key": "/events/mykey",
  17. "size": 502,
  18. "eTag": "091820398091823",
  19. "sequencer": "1123123"
  20. }
  21. }
  22. }
  23. ]
  24. }

// 只想返回Key的值
type Object struct {
Key string json:"key"
}

英文:

I have the Json structure below, and i'm trying to parse only the key field inside the object. It's possible to do it without mapping the complete structure?

  1. {
  2. "Records":[
  3. {
  4. "eventVersion":"2.0",
  5. "s3":{
  6. "s3SchemaVersion":"1.0",
  7. "configurationId":"my-event",
  8. "bucket":{
  9. "name":"super-files",
  10. "ownerIdentity":{
  11. "principalId":"41123123"
  12. },
  13. "arn":"arn:aws:s3:::myqueue"
  14. },
  15. "object":{
  16. "key":"/events/mykey",
  17. "size":502,
  18. "eTag":"091820398091823",
  19. "sequencer":"1123123"
  20. }
  21. }
  22. }
  23. ]
  24. }
  25. // Want to return only the Key value
  26. type Object struct {
  27. Key string `json:"key"`
  28. }

答案1

得分: 3

有几个第三方的JSON库非常快速,可以从JSON字符串中提取特定的值。

库:

GJSON示例:

  1. const json = `your json string`
  2. func main() {
  3. keys := gjson.Get(json, "Records.#.s3.object.key")
  4. // 返回所有记录的键的切片
  5. singleKey := gjson.Get(json, "Records.1.s3.object.key")
  6. // 只返回第一条记录的键
  7. }
英文:

There are a few 3rd party json libraries which are very fast at extracting only some values from your json string.

Libraries:

GJSON example:

  1. const json = `your json string`
  2. func main() {
  3. keys := gjson.Get(json, "Records.#.s3.object.key")
  4. // would return a slice of all records' keys
  5. singleKey := gjson.Get(json, "Records.1.s3.object.key")
  6. // would only return the key from the first record
  7. }

答案2

得分: 2

对象是S3的一部分,所以我创建了以下结构体,并且能够读取键值。

  1. type Root struct {
  2. Records []Record `json:"Records"`
  3. }
  4. type Record struct {
  5. S3 SS3 `json:"s3"`
  6. }
  7. type SS3 struct {
  8. Obj Object `json:"object"`
  9. }
  10. type Object struct {
  11. Key string `json:"key"`
  12. }
英文:

Object is part of S3, so I created struct as below and I was able to read key

  1. type Root struct {
  2. Records []Record `json:"Records"`
  3. }
  4. type Record struct {
  5. S3 SS3 `json:"s3"`
  6. }
  7. type SS3 struct {
  8. Obj Object `json:"object"`
  9. }
  10. type Object struct {
  11. Key string `json:"key"`
  12. }

huangapple
  • 本文由 发表于 2017年8月24日 11:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/45852398.html
匿名

发表评论

匿名网友

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

确定