我有一个样本如下,
result = [{"Key":"9802", "Record":{"action":"Warning","status":"Created","statusid":"9802","system":"CRM","thresholdtime":"9"}}]我怎样才能在歌郎中获得阈值呢?
我试着像这样展示:result[0]["Record"]["thresholdtime"]
error: invalid operation: result[0]["Record"] (type byte does not support indexing)谢谢
发布于 2018-09-20 21:12:45
Json.Unmarshal(.)示例会让你开始的。
这里有一种方法(围棋游乐场):
func main() {
var krs []KeyRecord
err := json.Unmarshal([]byte(jsonstr), &krs)
if err != nil {
panic(err)
}
fmt.Println(krs[0].Record.ThresholdTime)
// 9
}
type KeyRecord struct {
Key int `json:"Key,string"`
Record Record `json:"Record"`
}
type Record struct {
Action string `json:"action"`
Status string `json:"status"`
StatusId int `json:"statusid,string"`
System string `json:"system"`
ThresholdTime int `json:"thresholdtime,string"`
}
var jsonstr = `
[
{
"Key": "9802",
"Record": {
"action": "Warning",
"status": "Created",
"statusid": "9802",
"system": "CRM",
"thresholdtime": "9"
}
}
]
`您可以将JSON文档解封为泛型类型;但是,由于许多原因不建议使用JSON文档,最终与类型信息的丢失有关:
xs := []map[string]interface{}{}
err := json.Unmarshal([]byte(jsonstr), &xs)
if err != nil {
panic(err)
}
ttstr := xs[0]["Record"].(map[string]interface{})["thresholdtime"].(string)
fmt.Printf("%#v\n", ttstr) // Need to convert to int separately, if desired.
// "9"发布于 2018-09-20 21:08:48
像这样的事情应该会让你离我很近,我想:https://play.golang.org/p/ytpHTTNMjB-
使用内置的json包将数据解码为结构(附带json标记)。然后,它就像访问struct字段一样简单。
发布于 2018-09-20 23:29:22
使用json.Unmarshal将数据解编组成合适的数据类型。在许多情况下,您可以(我建议)使用自定义声明的带有struct标记的json类型来实现这个目的。
但是,对于另一个答案的注释,可以将其解封为一个interface{},并让解组程序确定表示JSON结构的最合适的数据类型。例如,[]interface{}类型的一个片段将表示一个列表、一个map[string]interface{}映射(一个字典)、它们的等价JSON的基元类型等等。
上周,我编写了一个解析器,它将这种方法用于另一个堆栈问题。这并不打算成为高性能或经过高度测试的代码,但是演示了关键要点:
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"strconv"
"strings"
)
// Some arbitrary JSON
const js = `
{
"key1": [
{"key2": false, "some_other_key": "abc"},
{"key3": 3}
],
"key2": {
"hello": "world"
},
"shallow": true,
"null_value": null
}`
func indentStringLines(s string, n int) string {
// Build indent whitespace - this has not been optimized!
var indent string
for i := 0; i < n; i++ {
indent += " "
}
parts := strings.Split(s, "\n")
for i := 0; i < len(parts) - 1; i++ {
parts[i] = indent + parts[i]
}
return strings.Join(parts, "\n")
}
func recursivelyPrintSlice(m []interface{}, indent int) string {
var str string
for i, val := range m {
str += fmt.Sprintf("%s: %s\n",
strconv.FormatInt(int64(i), 10),
recursivelyPrint(val, indent),
)
}
return strings.TrimSpace(str)
}
func recursivelyPrint(val interface{}, indent int) string {
var str string
switch v := val.(type) {
case bool:
str += strconv.FormatBool(v)
case float64:
str += strconv.FormatFloat(v, 'g', -1, 64)
case string:
str += v
case map[string]interface{}:
str += "{\n"
for key, childVal := range v {
str += fmt.Sprintf("%s: %s\n", key, recursivelyPrint(childVal, indent))
}
str += "}"
case []interface{}:
str += "[\n" + recursivelyPrintSlice(v, indent) + "\n]"
case nil:
str += "null"
default:
str += fmt.Sprintf(
"[unimplemented type printer for %s]",
reflect.ValueOf(v).Kind(),
)
}
return strings.TrimSpace(indentStringLines(str, indent+2))
}
func main() {
var x interface{}
err := json.Unmarshal([]byte(js), &x)
if err != nil {
log.Fatal(err)
}
fmt.Println(recursivelyPrint(x, 0))
}https://stackoverflow.com/questions/52433236
复制相似问题