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

如何使用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.

图片转代码服务由CSDN问答提供 功能建议

我具有以下图像的json结构:

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

我想将名称为“ active”的字段更改为“ inactive”而不更改 存储在字段中的值,如下所示:

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

有人可以解决这个问题吗?如果您可以在操场上展示,那就太好了。

我很难附加新值

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

如何将此添加到解决方案 提供给我的离子??应在使所有活动键变为非活动状态的末尾附加。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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条)

相关推荐 更多相似问题