简体   繁体   English

如何在 Go 的结构中打印出 JSON 的特定部分?

[英]How can I print out a specific part of my JSON within a struct in Go?

I want to print out the first "row" of my JSON that is within a struct in Go.我想打印出 Go 结构中的 JSON 的第一“行”。 The JSON looks like JSON 看起来像

[
   {
      "id":"7",
      "username":"user",
      "subject":"subject",
      "message":"message"
   },
   {
      "id":"6",
      "username":"user2",
      "subject":"subject2",
      "message":"message2"
   },
   {
      "id":"5",
      "username":"user3",
      "subject":"subject3",
      "message":"message3"
   },
   {
      "id":"4",
      "username":"user4",
      "subject":"subject4",
      "message":"message4"
   },
   {
      "id":"3",
      "username":"user5",
      "subject":"subject5",
      "message":"message5"
   },
   {
      "id":"2",
      "username":"user6",
      "subject":"subject6",
      "message":"message6"
   },
   {
      "id":"1",
      "username":"user7",
      "subject":"subject7",
      "message":"message7"
   }
]

I have put it in a Struct like this我把它放在这样的结构中

type Info struct {
    Id string
    Username string
    Subject string
    Message string
}
infoJson := html;
var information []Info;
err2 := json.Unmarshal([]byte(infoJson), &information);
if err2 != nil {
    fmt.Println(err2);
}

And then I can print all of them out using然后我可以使用

for _, info := range information {
    fmt.Println(info.Id + " " + info.Username);
    fmt.Println(info.Subject);
    fmt.Println(info.Message);
}

I would like to just be able to print out the JSON that is aligned with a specific id.我希望能够打印出与特定 ID 对齐的 JSON。 For example, I wish to be able to specify 7 and then all the things that are in the id:7 JSON row will be printed out in the above format.例如,我希望能够指定 7,然后 id:7 JSON 行中的所有内容都将以上述格式打印出来。 So it should print out:所以它应该打印出来:

7 user
subject
message

How can I do this?我怎样才能做到这一点?

If you want to print the "first" item.如果要打印“第一个”项目。 Then you can certainly use the index of the item to do that.然后你当然可以使用项目的索引来做到这一点。

fmt.Println(information[0])

If you want to print a specific item, then you would have to iterate using range and check if the item matches the condition.如果要打印特定项目,则必须使用范围进行迭代并检查该项目是否符合条件。

It may be more helpful to build a map of the items, in this case using ID as the key.构建项目的 map 可能更有帮助,在这种情况下使用 ID 作为键。

// Print the first item.
fmt.Println(information[0])

// Create a map to index items by ID.
dictionary := make(map[string]Info)
for _, info := range information {
    dictionary[info.Id] = info
}

element, ok := dictionary["7"]
if !ok {
    fmt.Println("Not found.")
    return
}
fmt.Println(element)

You can also add a method on Info to contain the formatting logic.您还可以在 Info 上添加一个方法来包含格式化逻辑。

func (i *Info) Print() string {
    return fmt.Sprintf("%s %s\n%s\n%s\n", i.Id, i.Username, i.Subject, i.Message)
}

And then simply call that:然后简单地调用它:

// Print the first item.
fmt.Println(information[0].Print())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM