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

用户462794:

我从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:这里si发送数据的代码(在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);

            },
        });


    },
});
PawełSzczur:

棘手的是如何通过来发送数据jQuery.ajax()文档中,您可能会发现:

默认情况下,作为对象传递给data选项的数据(从技术上讲,不是字符串)将被处理并转换为查询字符串,以适合默认的内容类型“ application / x-www-form-urlencoded” 。

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

lat=48.892423&lng=2.215331&acc=1962

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

Docker返回“ json:无法将字符串解组为[] string类型的Go值”

无法将字符串解组为struct类型的Go值

得到:无法将字符串解组为Go值

json:无法将数组解组为main类型的Go值

json:无法将数组解组为struct类型的Go值

无法将字符串解组为int64类型的Go值

Golang-无法将数字解组为字符串类型的Go值

JSON无法将对象解组为字符串类型的GO值

golang:解组:json:无法将数组解组为main.MonitorServerInfo类型的Go值

json:无法将字符串解组到Go struct字段中

如何使用go的json标签将数字和字符串属性解组为字符串值

无法在 Go 中将 !!seq 解组为字符串

panic:json:无法将数组解组为main类型的Go值。

如果将值为null的字符串编码的json int解组,则使用null之前的值

始终将JSON解组为某些类型(字符串)

获取错误:无法解析 Dockerrun JSON 文件:json:字符串结构标记的使用无效,试图将未加引号的值解组为 int

无法将字符串解组到类型模型的Go struct字段Article.article_type中。

将json数组解组到go struct中(数组位于JSON字符串的中间

无法将字符串解组到Go struct字段中

如何使用 go-mongo-driver 将 BSON 字符串值解组为自定义类型?

Go无法将字符串设置为struct

Terraform:“错误解码 JSON:json:无法将字符串解组到 Go struct 字段 ContainerDefinition”aws_ecs_task_definition 资源

kubectl 返回不能将字符串解组为 map[string]interface {} 类型的 Go 值

无法从XML解组非字符串值的字段

无法将类型为“字符串”的值分配为类型为“ UILabel?”

将GO YAML解组到地图或字符串

无法将json文件字典中的重复字符串解析为键/值对

如何在Go中解组转义的JSON字符串?