解析嵌套的 JSON 数组。

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

GO parse nested json array

问题

以下是翻译好的内容:

type response struct {
    META struct {
        LV []struct {
            RESPONSE struct {
                Data []struct {
                    array []struct {
                        val []string
                    }
                } `json:"data"`
            } `json:"response"`
        } `json:"lv1"`
    } `json:"meta"`
}

我该如何获取以下内容中的值?

"data": [
    ["1", "2", "3"]
]

我尝试过使用接口和结构体。使用接口的结果是接口类型的[1 2 3],但我不确定如何获取这些值。当使用结构体时,尝试映射一个数组的数组时遇到了问题,错误信息如下:

"cannot unmarshal array into Go struct field .data of type struct {
vals []string }"

英文:
{
    "meta": {
       "type": "RESPONSE",
       "application": "",
       "data0": {
         some data
       },
      "lv1": [
      {
        "status": "SUCCESS",
        "response-type": "JSON",
        "message": {},
        "response": {
        "more_data": "TRUE",
        "no_result": "5",
        "current_page": "1",
        "data": [[
         "1",
         "2", 
         "3"]]
        }
      }
     ]
   }
}

type response struct {
	META struct {
		LV []struct {
			RESPONSE struct {
				Data []struct {
					array []struct {
						val []string
					}
				} `json:"data"`
			} `json:"response"`
		} `json:"lv1"`
	} `json:"meta"`

}

How can I get the values in the following?

"data": [[
         "1",
         "2", 
         "3"]]

I've tried both interface and struct. Using interface results in [1 2 3] of interface type and I'm not sure how I can get the values. When using struct, I ran into problem when trying to map an array of array with error message:

> "cannot unmarshal array into Go struct field .data of type struct {
> vals []string }"

答案1

得分: 14

这是一个字符串的数组的数组,而不是包含字符串数组的结构体的数组,所以你可能更希望像这样定义:

type response struct {
    Meta struct {
        Lv []struct {
            Response struct {
                Data [][]string `json:"data"`
            } `json:"response"`
        } `json:"lv1"`
    } `json:"meta"`
}

(我还将全大写的字段名更改为符合 Go 代码风格的样式。)

值得一提的是,有一个方便的 JSON 转 Go 工具 这里,在删除 some data 部分(这使得 JSON 无效)后,它为你的输入生成了以下代码:

type AutoGenerated struct {
    Meta struct {
        Type        string `json:"type"`
        Application string `json:"application"`
        Data0       struct {
        } `json:"data0"`
        Lv1 []struct {
            Status       string `json:"status"`
            ResponseType string `json:"response-type"`
            Message      struct {
            } `json:"message"`
            Response struct {
                MoreData    string     `json:"more_data"`
                NoResult    string     `json:"no_result"`
                CurrentPage string     `json:"current_page"`
                Data        [][]string `json:"data"`
            } `json:"response"`
        } `json:"lv1"`
    } `json:"meta"`
}
英文:

It's an array of arrays of strings, not an array of structs containing arrays of structs containing strings, so you want something more like:

type response struct {
    Meta struct {
        Lv []struct {
            Response struct {
                Data [][]string `json:"data"`
            } `json:"response"`
        } `json:"lv1"`
    } `json:"meta"`
}

(I also changed the all-caps field names to match expected Go code style.)

For what it's worth, there is a handy JSON-to-Go tool here which gave me this for your input, after deleting the some data bit (which made the JSON invalid):

type AutoGenerated struct {
	Meta struct {
		Type        string `json:"type"`
		Application string `json:"application"`
		Data0       struct {
		} `json:"data0"`
		Lv1 []struct {
			Status       string `json:"status"`
			ResponseType string `json:"response-type"`
			Message      struct {
			} `json:"message"`
			Response struct {
				MoreData    string     `json:"more_data"`
				NoResult    string     `json:"no_result"`
				CurrentPage string     `json:"current_page"`
				Data        [][]string `json:"data"`
			} `json:"response"`
		} `json:"lv1"`
	} `json:"meta"`
}

huangapple
  • 本文由 发表于 2017年9月9日 04:40:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/46124232.html
匿名

发表评论

匿名网友

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

确定