你好!要翻译的内容是:“How can I structure the interface?”

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

How can I structure the interface?

问题

我有这个 JSON 数组,我需要提取数据:

b := [[{"client": "321"}], [{"number": "3123"}]]

我该如何构建接口?

var f interface{}
err := json.Unmarshal(b, &f)

f = map[string]interface{}{

----> ?

}

你需要将 JSON 数据解析为一个接口类型的变量,然后将其转换为一个 map[string]interface{} 类型的变量。这样你就可以通过键值对的方式来访问和提取数据了。

英文:

I have this json array and I need to extract the data:

b := [[{"client": " 321"}], [{"number": "3123"}]]

How can I structure the interface?

var f interface{}
err := json.Unmarshal(b, &f)

f = map[string]interface{}{

----> ?

}

答案1

得分: 0

这是你要找的内容吗?

你可以在这里测试代码。

package main

import (
	"encoding/json"
	"fmt"
	"log"
)

func main() {
	// 测试输入(json.Unmarshal 需要 []byte)
	b := []byte("[[{\"client\": \" 321\"}], [{\"number\": \"3123\"}]]")

	// 声明目标变量的正确格式
	var f [][]map[string]string

	// 反序列化json
	err := json.Unmarshal(b, &f)
	if err != nil {
		// 处理错误
		log.Fatal(err)
	}

	// 输出结果
	fmt.Println(f)
}

有关详细信息,请参阅代码中的注释。如果有问题,请随时提问。

英文:

Is this what you are looking for?

You can test the code here.

package main

import (
	"encoding/json"
	"fmt"
	"log"
)

func main() {
	// test input (json.Unmarshal expects []byte)
	b := []byte("[[{\"client\": \" 321\"}], [{\"number\": \"3123\"}]]")

	// declare the target variable in the correct format
	var f [][]map[string]string

	// unmarshal the json
	err := json.Unmarshal(b, &f)
	if err != nil {
		// handle error
		log.Fatal(err)
	}

	// output result
	fmt.Println(f)
}

For details see the comments in the code. Feel free to ask.

huangapple
  • 本文由 发表于 2017年8月13日 01:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/45653222.html
匿名

发表评论

匿名网友

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

确定