json: 无法将字符串解组为类型为 main.test_struct 的 Go 值

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

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}"

这是我的代码:

  1. type test_struct struct {
  2. Lat float32 `json:"lat"`
  3. Lng float32 `json:"lng"`
  4. Acc int `json:"acc"`
  5. }
  6. func postGeo(w http.ResponseWriter, r *http.Request) {
  7. var t test_struct
  8. err := json.NewDecoder(r.Body).Decode(&t)
  9. if err != nil {
  10. panic(err)
  11. }
  12. /*hah, err := ioutil.ReadAll(r.Body);
  13. if err != nil {
  14. panic(err)
  15. }
  16. Info.Println(hah)
  17. s := string(hah)
  18. Info.Println(s)
  19. Info.Println(t.Lat)*/
  20. defer r.Body.Close()
  21. Info.Println("POST FP")
  22. w.Header().Set("Access-Control-Allow-Origin", "*")
  23. fmt.Fprintf(w, "200")
  24. }

如果有人有任何线索...
谢谢和问候

编辑:第二个版本仍然出现相同的错误:

  1. type test_struct struct {
  2. Lat float32 `json:"lat"`
  3. Lng float32 `json:"lng"`
  4. Acc int `json:"acc"`
  5. }
  6. func postGeo(w http.ResponseWriter, r *http.Request) {
  7. var t test_struct
  8. err := json.NewDecoder(r.Body).Decode(&t)
  9. if err != nil {
  10. panic(err)
  11. }
  12. /*hah, err := ioutil.ReadAll(r.Body);
  13. if err != nil {
  14. panic(err)
  15. }
  16. Info.Println(hah)
  17. s := string(hah)
  18. Info.Println(s)
  19. Info.Println(t.Lat)*/
  20. //defer r.Body.Close()
  21. fmt.Println("POST FP")
  22. w.Header().Set("Access-Control-Allow-Origin", "*")
  23. fmt.Fprintf(w, "200")
  24. }

编辑ter:
这是发送数据的代码(使用JavaScript):

  1. var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=666";
  2. $.ajax({
  3. type: 'POST',
  4. url: url,
  5. crossDomain: true,
  6. success: function(data){
  7. //success jsonp handler - assume content in data.response
  8. console.log(data);
  9. var long = data.location.lng ;
  10. var lat = data.location.lat;
  11. var params = {long:long, lat:lat};
  12. url_bis = "http://localhost:9280/post_geo/";
  13. $.ajax({
  14. type: 'POST',
  15. url: url_bis,
  16. crossDomain: true,
  17. data: params,
  18. dataType: 'jsonp',
  19. success: function(data2){
  20. console.log(data2);
  21. },
  22. });
  23. },
  24. });
英文:

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 :

  1. type test_struct struct {
  2. Lat float32 `json:"lat"`
  3. Lng float32 `json:"lng"`
  4. Acc int `json:"acc"`
  5. }
  6. func postGeo(w http.ResponseWriter, r *http.Request) {
  7. var t test_struct;
  8. err := json.NewDecoder(r.Body).Decode(&t)
  9. if err != nil {
  10. panic(err)
  11. }
  12. /* hah, err := ioutil.ReadAll(r.Body);
  13. if err != nil {
  14. panic(err)
  15. }
  16. Info.Println(hah)
  17. s := string(hah)
  18. Info.Println(s)
  19. Info.Println(t.Lat)*/
  20. defer r.Body.Close()
  21. Info.Println("POST FP")
  22. w.Header().Set("Access-Control-Allow-Origin", "*")
  23. fmt.Fprintf(w, "200")
  24. }

If anyone have any clue ...
Thanks and regards

edit : Second version still the same error :

  1. type test_struct struct {
  2. Lat float32 `json:"lat"`
  3. Lng float32 `json:"lng"`
  4. Acc int `json:"acc"`
  5. }
  6. func postGeo(w http.ResponseWriter, r *http.Request) {
  7. var t test_struct;
  8. err := json.NewDecoder(r.Body).Decode(&t)
  9. if err != nil {
  10. panic(err)
  11. }
  12. /* hah, err := ioutil.ReadAll(r.Body);
  13. if err != nil {
  14. panic(err)
  15. }
  16. Info.Println(hah)
  17. s := string(hah)
  18. Info.Println(s)
  19. Info.Println(t.Lat)*/
  20. //defer r.Body.Close()
  21. fmt.Println("POST FP")
  22. w.Header().Set("Access-Control-Allow-Origin", "*")
  23. fmt.Fprintf(w, "200")
  24. }

edit ter :
here si the code that send the data (in javascript)

  1. var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=666";
  2. $.ajax({
  3. type: 'POST',
  4. url: url,
  5. crossDomain: true,
  6. success: function(data){
  7. //success jsonp handler - assume content in data.response
  8. console.log(data);
  9. var long = data.location.lng ;
  10. var lat = data.location.lat;
  11. var params = {long:long, lat:lat};
  12. url_bis = "http://localhost:9280/post_geo/";
  13. $.ajax({
  14. type: 'POST',
  15. url: url_bis,
  16. crossDomain: true,
  17. data: params,
  18. dataType: 'jsonp',
  19. success: function(data2){
  20. console.log(data2);
  21. },
  22. });
  23. },
  24. });

答案1

得分: 2

复杂的部分是数据如何通过jQuery.ajax()发送。在文档中,您可以找到以下内容:

> 默认情况下,作为对象传递给数据选项的数据(严格来说,除了字符串以外的任何内容)将被处理和转换为查询字符串,以适应默认的内容类型“application/x-www-form-urlencoded”。

这意味着脚本发送的数据看起来更像是:

  1. 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:

  1. lat=48.892423&lng=2.215331&acc=1962

huangapple
  • 本文由 发表于 2017年2月4日 01:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/42029418.html
匿名

发表评论

匿名网友

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

确定