dream5694 2017-02-03 17:06
浏览 52
已采纳

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

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条回答 默认 最新

  • dongzexi5125 2017-02-03 18:04
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?