英文:
json: cannot unmarshal string into Go value of type main.test_struct
问题
我从一个API接收到一个JSON,然后尝试对其进行解组,但我不理解出现的错误:
json: 无法将字符串解组为类型为main.test_struct的Go值
这是我收到的JSON:
INFO: 2017/02/03 17:47:53 ApiRecordGeo.go:66: "{"lat":48.892423,"lng":2.215331,"acc":1962}"
这是我的代码:
type test_struct struct {
Lat float32 `json:"lat"`
Lng float32 `json:"lng"`
Acc int `json:"acc"`
}
func postGeo(w http.ResponseWriter, r *http.Request) {
var t test_struct
err := json.NewDecoder(r.Body).Decode(&t)
if err != nil {
panic(err)
}
/*hah, err := ioutil.ReadAll(r.Body);
if err != nil {
panic(err)
}
Info.Println(hah)
s := string(hah)
Info.Println(s)
Info.Println(t.Lat)*/
defer r.Body.Close()
Info.Println("POST FP")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "200")
}
如果有人有任何线索...
谢谢和问候
编辑:第二个版本仍然出现相同的错误:
type test_struct struct {
Lat float32 `json:"lat"`
Lng float32 `json:"lng"`
Acc int `json:"acc"`
}
func postGeo(w http.ResponseWriter, r *http.Request) {
var t test_struct
err := json.NewDecoder(r.Body).Decode(&t)
if err != nil {
panic(err)
}
/*hah, err := ioutil.ReadAll(r.Body);
if err != nil {
panic(err)
}
Info.Println(hah)
s := string(hah)
Info.Println(s)
Info.Println(t.Lat)*/
//defer r.Body.Close()
fmt.Println("POST FP")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "200")
}
编辑ter:
这是发送数据的代码(使用JavaScript):
var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=666";
$.ajax({
type: 'POST',
url: url,
crossDomain: true,
success: function(data){
//success jsonp handler - assume content in data.response
console.log(data);
var long = data.location.lng ;
var lat = data.location.lat;
var params = {long:long, lat:lat};
url_bis = "http://localhost:9280/post_geo/";
$.ajax({
type: 'POST',
url: url_bis,
crossDomain: true,
data: params,
dataType: 'jsonp',
success: function(data2){
console.log(data2);
},
});
},
});
英文:
I receive a json from an api and i try to unmarshall it, and i don't understand the error that i get :
> json: cannot unmarshal string into Go value of type main.test_struct
Here is the json that i get :
> INFO: 2017/02/03 17:47:53 ApiRecordGeo.go:66: "{"lat":48.892423,"lng":2.215331,"acc":1962}"
here is my code :
type test_struct struct {
Lat float32 `json:"lat"`
Lng float32 `json:"lng"`
Acc int `json:"acc"`
}
func postGeo(w http.ResponseWriter, r *http.Request) {
var t test_struct;
err := json.NewDecoder(r.Body).Decode(&t)
if err != nil {
panic(err)
}
/* hah, err := ioutil.ReadAll(r.Body);
if err != nil {
panic(err)
}
Info.Println(hah)
s := string(hah)
Info.Println(s)
Info.Println(t.Lat)*/
defer r.Body.Close()
Info.Println("POST FP")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "200")
}
If anyone have any clue ...
Thanks and regards
edit : Second version still the same error :
type test_struct struct {
Lat float32 `json:"lat"`
Lng float32 `json:"lng"`
Acc int `json:"acc"`
}
func postGeo(w http.ResponseWriter, r *http.Request) {
var t test_struct;
err := json.NewDecoder(r.Body).Decode(&t)
if err != nil {
panic(err)
}
/* hah, err := ioutil.ReadAll(r.Body);
if err != nil {
panic(err)
}
Info.Println(hah)
s := string(hah)
Info.Println(s)
Info.Println(t.Lat)*/
//defer r.Body.Close()
fmt.Println("POST FP")
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "200")
}
edit ter :
here si the code that send the data (in javascript)
var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=666";
$.ajax({
type: 'POST',
url: url,
crossDomain: true,
success: function(data){
//success jsonp handler - assume content in data.response
console.log(data);
var long = data.location.lng ;
var lat = data.location.lat;
var params = {long:long, lat:lat};
url_bis = "http://localhost:9280/post_geo/";
$.ajax({
type: 'POST',
url: url_bis,
crossDomain: true,
data: params,
dataType: 'jsonp',
success: function(data2){
console.log(data2);
},
});
},
});
答案1
得分: 2
复杂的部分是数据如何通过jQuery.ajax()
发送。在文档中,您可以找到以下内容:
> 默认情况下,作为对象传递给数据选项的数据(严格来说,除了字符串以外的任何内容)将被处理和转换为查询字符串,以适应默认的内容类型“application/x-www-form-urlencoded”。
这意味着脚本发送的数据看起来更像是:
lat=48.892423&lng=2.215331&acc=1962
英文:
The tricky part is how the data is sent by a jQuery.ajax()
. In a documentation you may find:
> By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".
Which means that data the script is sending looks more like:
lat=48.892423&lng=2.215331&acc=1962
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论