How to delete a part of from json file golang

huangapple go评论80阅读模式
英文:

How to delete a part of from json file golang

问题

假设我有这样一个 JSON 文件。根据用户的选择,我想删除其中的 window、image 或 text 字段,并将剩余的内容打印到另一个文件中。

例如,我想删除 window 字段,并将剩余内容写入另一个文件。

我陷入困境,不知道该如何做。写入另一个文件很容易,但我不知道如何删除一个字段。

你能给我一些建议吗?

以下是删除 window 字段后的内容:

{
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}

英文:

Suppose I have such a json file. I want to delete one of the window, image or text fields according to the user's choice and print the rest of the content to a different file.

  {
 "window": {
    "title": "Sample Konfabulator Widget",
    "name": "main_window",
    "width": 500,
    "height": 500
},
"image": { 
    "src": "Images/Sun.png",
    "name": "sun1",
    "hOffset": 250,
    "vOffset": 250,
    "alignment": "center"
},
"text": {
    "data": "Click Here",
    "size": 36,
    "style": "bold",
    "name": "text1",
    "hOffset": 250,
    "vOffset": 100,
    "alignment": "center",
    "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}

For example, I want to delete the window field
I want to write this content to a different file.

I'm stuck and have no idea how to do it. It is easy to write to another file, but I have no way of knowing how to delete a fiel

Can you suggest me some approaches that make sense?

   {
  "image": { 
    "src": "Images/Sun.png",
    "name": "sun1",
    "hOffset": 250,
    "vOffset": 250,
    "alignment": "center"
},
"text": {
    "data": "Click Here",
    "size": 36,
    "style": "bold",
    "name": "text1",
    "hOffset": 250,
    "vOffset": 100,
    "alignment": "center",
    "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}

答案1

得分: 1

根据我的理解,我们可以采用两种方法来处理这个问题。
首先,我们可以将JSON数据解组为Go语言的struct结构。
其次,我们可以将JSON数据解组为Go语言的map结构。
因为我不知道具体的结构,所以我们可以选择使用map。

你也可以尝试第一种方法来进行学习,但是那只适用于正确的结构。

data := []byte(`{"window":{"title":"Sample Konfabulator Widget","name":"main_window","width":500,"height":500},"image":{"src":"Images/Sun.png","name":"sun1","hOffset":250,"vOffset":250,"alignment":"center"},"text":{"data":"Click Here","size":36,"style":"bold","name":"text1","hOffset":250,"vOffset":100,"alignment":"center","onMouseUp":"sun1.opacity = (sun1.opacity / 100) * 90;"}}`)

var v interface{}
err := json.Unmarshal(data, &v)
if err != nil {
    fmt.Println("error:", err)
}
m := v.(map[string]interface{})
// 删除window字段
delete(m, "window")

然后你可以再次将其编组为JSON并写入文件

```go
b, err := json.Marshal(m)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Println(string(b))

Go Playground

英文:

As per my understanding, we can follow two approaches here.
First We can Unmarshal JSON data into the Go language struct
Second, we can UnmarshalJSON data into the Go language map
because I don't know the struct so we can go with the map.

You can try the first approach also for learning purposes but that will only work with proper struct.

data := []byte(`{"window":{"title":"Sample Konfabulator Widget","name":"main_window","width":500,"height":500},"image":{"src":"Images/Sun.png","name":"sun1","hOffset":250,"vOffset":250,"alignment":"center"},"text":{"data":"Click Here","size":36,"style":"bold","name":"text1","hOffset":250,"vOffset":100,"alignment":"center","onMouseUp":"sun1.opacity = (sun1.opacity / 100) * 90;"}}`)

var v interface{}
err := json.Unmarshal(data, &v)
if err != nil {
	fmt.Println("error:", err)
}
m := v.(map[string]interface{})
// delete window
delete(m, "window")

then you can again marshal to JSON and write to file

b, err := json.Marshal(m)
if err != nil {
	fmt.Println("error:", err)
}
fmt.Println(string(b))

Go Playground

huangapple
  • 本文由 发表于 2022年12月12日 00:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74762396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定