doupiao1893 2018-02-09 14:55
浏览 62
已采纳

如何使用golang将活动字段更改为非活动字段以保持字段值不变?

I have the following json structure for image:

 {"image": [
  {
    "bytesize": "-2",
    "default": "redlight_drdefault"
  },
  {
    "active": "JV77tdMcHkChFaF2H9uoDHepPhUcrlprYEsQr-r-1lg=",
    "bytesize": "566806"
  }
]}

I want to change the the field named "active" to "inactive" without changing the value stored in the field like the following:

    {"image": [
  {
    "bytesize": "-2",
    "default": "redlight_drdefault"
  },
  {
    "inactive": "JV77tdMcHkChFaF2H9uoDHepPhUcrlprYEsQr-r-1lg=",
    "bytesize": "566806"
  }
]}

Do anyone has the solution how to do this??If u can show this on playground it will be great.

I am having difficulty in appending new value which is:

    mapactive := map[string]string{"active": "gj65vn768=", 
    "bytesize":"76878768" }

How to append this to the solutions provided to me???This should be appended at the end of making all active keys inactive.

  • 写回答

3条回答 默认 最新

  • du958642589 2018-02-09 15:30
    关注

    Golang allows direct conversion between structures that are identical in types, even if they differ in field tags. That is, you can convert directly between:

    type Active struct {
        Active   string `json:"active"`
        ByteSize string `json:"bytesize"`
    }
    

    and

    type Inactive struct {
        Active string `json:"inactive"`
        ByteSize string `json:"bytesize"`
    }
    

    Using the following:

    var a Active
    // decode JSON into a
    var b Inactive = Inactive(a) // direct conversion allowed
    

    However, your input JSON won't easily support this. Your input JSON is listing the image field as a list, despite the various entries in the list not actually being identical in type or fields. The only real option in that case is to parse it as a nested set of map[string]interface{}:

    https://play.golang.org/p/4NgiKLlHg8c

    parsed := make(map[string]interface{})
    if err := json.Unmarshal([]byte(input), &parsed); err != nil {
        panic(err)
    }
    
    img, ok := parsed["image"]
    if ok {
        if imglist, ok := img.([]interface{}); ok {
            for i, mi := range imglist {
                if m, ok := mi.(map[string]interface{}); ok {
                    for k, v := range m {
                        if k == "active" {
                            m["inactive"] = v
                            delete(m, "active")
                            imglist[i] = m
                        }
                    }
                }
            }
        }
    }
    
    b, err := json.Marshal(img)
    if err != nil {
        panic(err)
    }
    

    Note that I've removed a lot of sanity checks from this code to make it shorter and more readable. Using nested interface maps for handling JSONs is inherently messy. If you can change how the input JSON is formatted, you'll have a lot better luck, and more easily maintainable code, by doing so.

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

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab