英文:
Golang parse JSON returns 0
问题
我正在尝试解析的JSON非常基本,看起来像这样。
{"id": 3, "title":"Test"}
以下是我尝试使用的创建和解析JSON的代码。
package main
import (
"fmt"
"encoding/json"
)
type Config struct{
id int
title string
}
func main() {
var jsonStr = []byte(`{"id": 3, "title":"Test"}`)
var conf Config
err := json.Unmarshal(jsonStr, &conf)
if err!=nil{
fmt.Print("Error:",err)
}
fmt.Println(conf)
fmt.Println(string(jsonStr))
}
我查看了很多不同的代码示例,但看不出我做错了什么。当我尝试运行时,返回的结果如下。
{0 }
{"id": 3, "title":"Test"}
我已经验证了JSON的有效性,但在尝试使用json.Unmarshal时仍然得到一个空的返回值。有关我漏掉的内容,你有什么想法,以便我可以解析这个JSON?
编辑:看起来如果我将标题大写(Id,Title),我可以使其工作。不幸的是,我正在测试的返回值是来自一个以小写形式返回所有内容的API。我需要能够解析上面列出的具有小写标题的JSON。
英文:
The JSON that I am at tempting to parse is very basic and looks like this.
{"id": 3, "title":"Test"}
The following is the code that I am attempting to use for creating and parsing the JSON.
package main
import (
"fmt"
"encoding/json"
)
type Config struct{
id int
title string
}
func main() {
var jsonStr = []byte(`{"id": 3, "title":"Test"}`)
var conf Config
err := json.Unmarshal(jsonStr, &conf)
if err!=nil{
fmt.Print("Error:",err)
}
fmt.Println(conf)
fmt.Println(string(jsonStr))
}
Been looking over a lot of different code examples and can't see what I'm doing wrong. When I attempt to run this, this is what I get as a return.
{0 }
{"id": 3, "title":"Test"}
I have verified that the JSON is valid, but continue to get an empty return when attempting to use json.Unmarshal. Any ideas on what I am missing so that I can get this JSON parsed?
EDIT: Looks like I can get this to work if I capitalize the titles (Id, Title). Unfortunately the return I am testing for is a return from an API which returns everything in lowercase. I need to be able to parse this JSON with lowercase titles as listed above.
答案1
得分: 3
你的Config
结构体的字段需要被导出(大写),但是你的JSON对象中的键可以保持小写。
参考链接:http://play.golang.org/p/0A5tkCkSO5
英文:
Your Config
struct's fields need to be exported (upper-case), but the keys in your JSON object may remain lower-case.
See here: http://play.golang.org/p/0A5tkCkSO5
答案2
得分: 2
请参考JSON包文档,值得一读。虽然Amit已经解决了导出问题,但我将解决以下问题:
> 编辑:看起来如果我将标题(Id,Title)大写,我可以使其工作。不幸的是,我正在测试的返回值是来自一个将所有内容都转换为小写的API返回值。我需要能够解析具有上述小写标题的JSON。
正如你可以想象的那样,encoding/json
的作者已经考虑到了这一点,所以我再次鼓励你在下次查阅文档时参考解决方案(在playground上的示例):
type Config struct {
Id int `json:"id"`
Title string `json:"title"`
}
英文:
Please consult the JSON package documentation, it is worth the read. While Amit already addressed the export issue I will address the following:
> EDIT: Looks like I can get this to work if I capitalize the titles (Id, Title). Unfortunately the return I am testing for is a return from an API which returns everything in lowercase. I need to be able to parse this JSON with lowercase titles as listed above.
As you might imagine, the authors of encoding/json
have thought of that, so again I encourage you to consult the documentation next time. The solution is this (Example on playground):
type Config struct {
Id int `json:"id"`
Title string `json:"title"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论