douzhan5058 2016-12-01 21:24
浏览 98
已采纳

从JSON提取元素

I have a function that returns JSON, and the return type is this:

map[string]interface{}

In my particular case, I know that the returned JSON looks like this:

{
  "someNumber": 1,
  "someString": "ee",
  "someArray": [
    "stringInArray",
    {
      "anotherNumber": 100,
      "anotherString": "asdf",
      "yetAnotherString": "qwer"
    }
  ]
}

I want to get the value of "stringInArray" and also "anotherString". I've searched for solutions for example from Go by Example and blog.golang.org but I've not been successful.

For example, given that res is the json returned from the function call, I tried this (from the go blog):

var f interface{}
b := []byte(`res` )
err2 := json.Unmarshal(b, &f)
if err2!=nil{
    log.Fatalf("Err unmarshalling: %v", err2)
}

m := f.( map[string]interface{} )

for k, v := range m {
    switch vv := v.(type) {
    case string:
        fmt.Println(k, "is string", vv)
    case int:
        fmt.Println(k, "is int", vv)
    case []interface{}:
        fmt.Println(k, "is an array:")
        for i, u := range vv {
            fmt.Println(i, u)
        }
    default:
        fmt.Println(k, "is of a type I don't know how to handle")
    }
}

(I know the above would not do exactly what I want but it is a start.) But when code execution hits b := []byte(res ), I get this error:

Err unmarshalling: invalid character 'r' looking for beginning of value

What is the most efficient method / best practice for obtaining the values? I'm open to any solution, not necessarily the approach above.

@sydnash Here is the code I promised in my response to your comment:

type LoginResponse2 struct {

    id float64
    jsonrpc string
    result struct {
        token string
        details struct {
            a float64
            b string
            c float64
        }
    }   
}

var resStruct2 LoginResponse2
// Convert result to json
b, _ :=json.Marshal(res)
fmt.Println( string(b) )
// results of Println:
{"id":1,"jsonrpc":"2.0","result":[ "myToken",{"a":someNumber,"b":"some string","c":someOtherNumber} ] }


// Unmarshall json into struct
err2 := json.Unmarshal(b, &resStruct2)
if err2 != nil {
    log.Fatalf("Error unmarshalling json: %v", err)
}

fmt.Println("Here is json unmarshalled into a struct")
fmt.Println( resStruct2 )
// results of Println
{0  { {0  0}}}
  • 写回答

2条回答 默认 最新

  • dongya0914 2016-12-02 01:02
    关注

    I think you should use b := []byte(res) instead b :=[]byte[res] and res should be a json string or []byte. res is not a legal json format.

    this information maybe help you: To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:

    bool, for JSON booleans

    float64, for JSON numbers

    string, for JSON strings

    []interface{}, for JSON arrays

    map[string]interface{}, for JSON objects

    nil for JSON null

    you can see there is no int but float64 for JSON numbers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色