【问题标题】:I was getting output of exec.Command output in the following manner. from that output I want to get data which I needed我以以下方式获得 exec.Command 输出的输出。从那个输出我想得到我需要的数据
【发布时间】:2021-02-03 04:16:59
【问题描述】:

从输出中我只需要带有 requestStatus Failed 部分的 json 数据。剩余的 json 数据应该被覆盖每个更新/可以删除。您能否建议我如何获取仅需要的数据。 源代码:我的源代码是这样的。

cmd := exec.Command(command, args...)
cmd.Dir = dir

var stdBuffer bytes.Buffer
mw := io.MultiWriter(os.Stdout, &stdBuffer)

cmd.Stdout = mw
cmd.Stderr = mw

// Execute the command
if err := cmd.Run(); err != nil {
    log.Panic(err)
}

log.Println(stdBuffer.String())

    
Output: this is how output looks for my input.

{
   "time": "10:26:03 AM",
   "requestId": 71795,
   "requestStatus": "ongoing",
   "requestMessage": "Waiting for response"
}
{
   "time": "10:26:08 AM",
   "requestId": 71795,
   "requestStatus": "ongoing",
   "requestMessage": "Waiting for response"
}
{
   "time": "10:26:13 AM",
   "requestId": 71795,
   "requestStatus": "ongoing",
   "requestMessage": "Waiting for response"
}
{
   "time": "10:26:14 AM",
   "requestId": 71795,
   "requestStatus": "failed",
   "requestMessage": {
      "ValidationResult": {
         "logs": {
            "Elements": null,
            "objectsErrors": null,
            "occurrencesErrors": null
         }
      }
    }
}

【问题讨论】:

  • 输出是一系列 JSON 对象。从中解组一系列 JSON 对象。你试过什么?你有什么问题?或者这是几次运行的输出?那么输出可能只是一个 JSON 对象。
  • 此输出仅用于单次运行。我只执行了一次命令,然后我得到了来自同行的响应。在最后(请求状态失败)的输出 json 对象中是来自对等方的有效输出。我试图解组收到的输出我收到错误,例如 unmarshal failed 'invalid character '{' is received'
  • 顶级值后的无效字符'{'"}
  • 我可以从任何人那里获得有关此问题的帮助吗?
  • 我发布了一个适合我的答案。尝试一下,如果它不适合你,请解释原因,并描述你得到了什么。

标签: json go exec stdout


【解决方案1】:

您不能使用 json.Unmarshal() 解组包含多个(独立)JSON 值的内容,例如您的输出(它是多个 JSON 对象的串联)。

使用json.Decoder 逐一解码流中的多个 JSON 值(对象)。

例如:

dec := json.NewDecoder(strings.NewReader(output))

var m map[string]interface{}
for {
    if err := dec.Decode(&m); err != nil {
        if err == io.EOF {
            break
        }
        panic(err)
    }
    fmt.Println("Decoded:", m)
}

这将输出(在Go Playground 上尝试):

Decoded: map[requestId:71795 requestMessage:Waiting for response requestStatus:ongoing time:10:26:03 AM]
Decoded: map[requestId:71795 requestMessage:Waiting for response requestStatus:ongoing time:10:26:08 AM]
Decoded: map[requestId:71795 requestMessage:Waiting for response requestStatus:ongoing time:10:26:13 AM]
Decoded: map[requestId:71795 requestMessage:map[ValidationResult:map[logs:map[Elements:<nil> objectsErrors:<nil> occurrencesErrors:<nil>]]] requestStatus:failed time:10:26:14 AM]

要解码来自您的stdBuffer 的内容,您可以将其传递给json.NewDecoder()

dec := json.NewDecoder(&stdBuffer)

如果您只需要输出具有"failed" 状态的对象,只需使用if 语句即可:

    if m["requestStatus"] == "failed" {
        fmt.Println("Decoded:", m)
    }

这只会输出(在Go Playground上试试):

Decoded: map[requestId:71795 requestMessage:map[ValidationResult:map[logs:map[Elements:<nil> objectsErrors:<nil> occurrencesErrors:<nil>]]] requestStatus:failed time:10:26:14 AM]

【讨论】:

  • 感谢您的建议。我得到了与您共享的相同的输出。但我只想要该系列的以下输出。解码:map[requestId:71795 requestMessage:map[ValidationResult:map[logs:map[Elements: objectsErrors: occurrencesErrors:]]] requestStatus:failed time:10:26:14 AM]
  • @Raghavendra 99% 的解决方案已交给您。请想一想。我更新了答案,只输出失败的状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多