{"id":86242,"date":"2024-01-04T17:16:26","date_gmt":"2024-01-04T09:16:26","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=86242"},"modified":"2024-01-04T17:16:26","modified_gmt":"2024-01-04T09:16:26","slug":"json-data","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/json-data.html","title":{"rendered":"json\u6570\u636e\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\njson\u7684\u5168\u79f0\u662fJavascript object notation, \u4e2d\u6587\u5168\u79f0\uff1ajs\u5bf9\u8c61\u8868\u793a\u6cd5\u3002\u5728\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u534f\u8bae\u4e2d\uff0c\u6709\uff1ajson\u3001xml\u3001yaml\u3001protocol buffer\u7b49\u3002\u5176\u4e2djson\u662f\u524d\u540e\u7aefAPI\u5951\u7ea6\u6570\u636e\u7684\u4e3b\u8981\u4f20\u8f93\u5f62\u5f0f\u3002json\u652f\u6301\u6570\u503c\u3001\u5e03\u5c14\u503c\u3001\u6570\u7ec4\u548c\u5bf9\u8c61\u56db\u79cd\u6570\u636e\u7c7b\u578b\u3002\u901a\u8fc7\u8fd9\u56db\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u6784\u5efa\u590d\u6742\u7684\u6570\u636e\u6a21\u578b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728Go\u8bed\u8a00\u4e2d\uff0c\u901a\u8fc7json\u6807\u51c6\u5e93\uff0c\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002\u5b83\u53ef\u4ee5\u5e8f\u5217\u5316map\uff0c\u7ed3\u6784\u4f53\u3001\u6570\u7ec4\u548cslice\u52a8\u6001\u6570\u7ec4\u3001\u5185\u7f6e\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3002<\/p>\n

\u6211\u4eec\u5728\u8fd9\u91cc\u4e3e\u4e09\u4e2a\u4f8b\u5b50\uff0c\u8bf4\u660ejson\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u7ecf\u5e38\u53ea\u7528\u5230\u4e00\u79cd\u7ed3\u6784\u4f53\u5bf9\u8c61\u63a5\u6536\u548c\u53d1\u9001\u6570\u636e\u3002<\/p>\n

\"\"<\/p>\n

\u7ed3\u6784\u4f53<\/strong><\/div>\n

type Person struct {
\nName string `json:\"name\"`
\nAge int `json:\"age\"`
\n}<\/code>
\n
\nfunc main() {
\nvar (
\nnewPer Person
\nbts []byte
\n)
\nper:=Person{
\nName: \"Lily\",
\nAge: 29,
\n}
\n\/\/ \u5e8f\u5217\u5316
\nif bts, err = json.Marshal(per); err !=nil{
\nlog.Fatal(err.Error())
\nreturn
\n}<\/code>
\n
\nif err = json.Unmarshal(bts, &newPer); err !=nil{
\nlog.Fatal(err.Error())
\nreturn
\n}
\nfmt.Println(newPer)
\n}<\/code><\/p>\n

map<\/strong><\/div>\n

func main() {
\nvar (
\nsmap = map[string]int{
\n\"Age\": 28,
\n\"Sex\": 1,
\n\"Floor\": 12,
\n}
\nnewSmap = make(map[string]int)
\nbts []byte
\n)
\nif bts, err = json.Marshal(smap); err !=nil {
\nlog.Fatal(err.Error())
\nreturn
\n}
\nif err = json.Umarshal(bts, &newSmap); err !=nil {
\nlog.Fatal(err.Error())
\nreturn
\n}
\nfmt.Println(newSmap)
\n}<\/code><\/p>\n

\u6570\u7ec4\u6216\u8005slice\u52a8\u6001\u6570\u7ec4<\/strong><\/div>\n

func main() {
\nvar (
\nages []int = []int{23, 20, 28, 25, 30}
\nbts []byte
\nerr error
\nnewAges = make([]int, len(ages))
\n)
\nif bts, err = json.Marshal(ages); err != nil {
\nfmt.Println(err.Error())
\nreturn
\n}
\nif err = json.Unmarshal(bts, &newAges); err != nil {
\nfmt.Println(err.Error())
\nreturn
\n}
\nfmt.Println(\"new ages: \", newAges)
\nreturn
\n}<\/code>
\n\u521d\u5b66\u8005\u5bf9\u7ed3\u6784\u4f53\u7684json\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u6bd4\u8f83\u6e05\u695a\uff0c\u5bf9\u540e\u4e24\u79cd\u53ef\u80fd\u5f88\u5c11\u7528\uff0c\u4e5f\u4e0d\u4f1a\u7528\u3002<\/p>\n