dtvpe4837413 2019-04-15 19:33
浏览 1078
已采纳

读取map [string] interface {}中的数据

Source server return the data in a Json format of multiple object interfaces, how can we parse such data ?

I am using a variable of JSON map[string]interface{} type to hold the result from server

 The data return from Server.
"data": [
       {
        "group": "PAA_TEST",
        "id": "2018-04-10T09:24:18.000000Z",
        "name": "PAA_STATION",
        "released": true,
        "version": 33
    },
    {
        "group": "PAA_TEST",
        "id": "2018-03-19T10:50:21.000000Z",
        "name": "PAA_STATION",
        "released": false,
        "version": 32
    }

my fmt.print output outputdata["data"] //where output data is of JSON map[string]interface{}

    [
       map[group:PAA_TEST id:2018-04-10T09:24:18.000000Z name:PAA_STATION 
       released:true version:33] 
       map[group:PAA_TEST id:2018-03-19T10:50:21.000000Z name:PAA_STATION 
       released:false version:32] 
   ]

How can we iterate with multiple Map interfaces? For example, if I just want to process the information with released status as true. I am trying various method for indexing but no luck yet.

  • 写回答

1条回答 默认 最新

  • dpd46554 2019-04-15 21:55
    关注

    The best solution is to decode the JSON directly to a Go type that matches the structure of the data. This avoids the type assertions required to dig through a map[string]interface{}.

    I'll assume that the common function looks something like this:

    func request(path string, ... more arguments) (map[string]interface{}}, error) {
          ...
          resp, err := client.Do(req)
          if err != nil {
             return nil, err
          }
          defer resp.Body.Close()
          ...
          var result map[string]interface{}
          err := json.NewDecoder(resp.Body).Decode(&result)
          return result, err
    }
    

    Change the function to take a pointer to the result as an argument:

    func request(pv interface{}, path string, ... more arguments) error {
          ...
          resp, err := client.Do(req)
          if err != nil {
             return err
          }
          defer resp.Body.Close()
          ...
          err := json.NewDecoder(resp.Body).Decode(pv)
          return err
    }
    

    Call this modified function like this:

    var result struct { 
        Data []struct{ 
            Group, ID, Name string
            Released bool
            Version int 
        }
    }
    err := request(&result, "some/path", ... more arguments )
    if err != nil {
        // handle error
    }
    
    for _, d := range result.Data {
       if !d.Released {
           continue
       }
       fmt.Println(d.Group, d.ID, d.Name, d.Version)
       ... process d
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!