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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?