If you convert json to Structure in Golang's UnMarshal and then to json in Marshall, the part of the array will become an object, so I want to maintain the array.

Asked 2 years ago, Updated 2 years ago, 75 views

What do you want to do

  • Convert {"color":"red", "mrkdwn_in":["text"]} to a structure in UnMarshal
  • Change color to red→blue
  • Change the modified structure from Marshall to json
  • Original "mrkdwn_in":["text"] does not maintain well
    • "mrkdwn_in": [{"text":"}] or "mrkdwn_in":".
  • I want to keep the mrkdwn_in part in its original state
  • "mrkdwn_in": [{"text":"}] or "mrkdwn_in":".

Code

package main

import(
    "encoding/json"
    "fmt"
)

type TestStructure {
    Color string `json:`color``
    MrkdwnIn[]MrkdwnIn`json: "mrkdwn_in"`
}

typeMrkdwnInstructure{
    Text string `json:`text``
}

funcmain(){
    jsonData1:="{\"color\":\"red\"',\"mrkdwn_in\":[\"text\"]}"
    varbody TestStruct
    json.Unmarshal([]byte(jsonData1), & body)
    body.Color="blue"
    varjsonData2[]byte
    jsonData2,_=json.Marshal(body)

    jsonDataText: =string(jsonData2)
    fmt.Println(jsonDataText)
}

Results of above code execution

Enter a description of the image here

go

2022-09-30 19:16

1 Answers

Now it's resolved.

MrkdwnIn[]MrkdwnIn`json: "mrkdwn_in"`

MrkdwnIn[]string `json: "mrkdwn_in"`


2022-09-30 19:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.