dpwh11290 2016-02-16 19:20
浏览 184
已采纳

在golang中无需结构即可对JSON数据进行最少的修改

I have a solr response in JSON format which looks like this:

 {
  "responseHeader": {
    "status": 0,
    "QTime": 0,
    "params": {
      "q": "solo",
      "wt": "json"
    }
  },
  "response": {
    "numFound": 2,
    "start": 0,
    "docs": [
      {
          <Large nested JSON element>
      },
      {
          <Large nested JSON element>
      }
    ]
  }
}

Now, in my Golang app, I would like to quickly remove the "responseHeader" so that I can return the "response" alone. How can I do this without creating large structures?


Edit 1

The answer by evanmcdonnal was the solution to this problem, but it had some minor typos, this is what I ended up using:

var temp map[string]interface{}

if err := json.Unmarshal(body, &temp); err != nil {
    panic(err.Error())
}

result, err := json.Marshal(temp["response"])
  • 写回答

1条回答 默认 最新

  • dsxz84851 2016-02-16 19:24
    关注

    Here's a really brief example of how to do this quickly and easily. The steps are; unmarshal into the universal map[string]interface{} then, assuming no errors, marshal only the inner object which you want.

    var temp := &map[string]interface{}
    
    if err := json.Unmarshal(input, temp); err != nil {
         return err;
    }
    
    return json.Marshal(temp["response"])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制