1).返回字段数等于struct数,见代码:

    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. "strings"
    6. )
    7. type GetResponse struct {
    8. Message string `json:message`
    9. Success int `json:success`
    10. Vild string `json:vild`
    11. Core string `json:core`
    12. }
    13. func main() {
    14. sss := `callback_1491381399454({"message": "success", "success": 1, "vild": "bc48a84c5b0175efb6a59b2036f76cd8", "core": "3"})`
    15. k := strings.Index(sss, "{") // 找第一个‘{’的位置
    16. l := strings.Index(sss, ")") // 找第一个‘)’的位置
    17. if k == -1 || l == -1 { // 是否查找成功
    18. return
    19. }
    20. sbss := sss[k:l]
    21. fmt.Printf("sss = ")
    22. fmt.Println(sss)
    23. fmt.Printf("sbss = ")
    24. fmt.Println(sbss)
    25. var geres GetResponse
    26. err := json.Unmarshal([]byte(sbss), &geres)
    27. if err != nil {
    28. fmt.Println(err.Error())
    29. return
    30. }
    31. fmt.Printf("geres = ")
    32. fmt.Println(geres)
    33. fmt.Printf("geres.Vild = %s", geres.Vild)
    34. }

    2).返回字段数小于定义struct数, 代码如下:

    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. "strings"
    6. )
    7. type GetResponse struct {
    8. Message string `json:message`
    9. Success int `json:success`
    10. Vild string `json:vild`
    11. }
    12. func main() {
    13. sss := `callback_1491381399454({"message": "success", "success": 1, "vild": "bc48a84c5b0175efb6a59b2036f76cd8", "core": "3"})`
    14. k := strings.Index(sss, "{") // 找第一个‘{’的位置
    15. l := strings.Index(sss, ")") // 找第一个‘)’的位置
    16. if k == -1 || l == -1 { // 是否查找成功
    17. return
    18. }
    19. sbss := sss[k:l]
    20. fmt.Printf("sss = ")
    21. fmt.Println(sss)
    22. fmt.Printf("sbss = ")
    23. fmt.Println(sbss)
    24. var geres GetResponse
    25. err := json.Unmarshal([]byte(sbss), &geres)
    26. if err != nil {
    27. fmt.Println(err.Error())
    28. return
    29. }
    30. fmt.Printf("geres = ")
    31. fmt.Println(geres)
    32. fmt.Printf("geres.Vild = %s", geres.Vild)
    33. }

    说明:
    当你接收的 json格式文本字段比定义的结构体字段多的时候,也可以解析成功,只是不能访问多出的字段