我以以下方式获取exec.Command输出的输出.从该输出中,我想获取我需要的数据 [英] I was getting output of exec.Command output in the following manner. from that output I want to get data which I needed

查看:59
本文介绍了我以以下方式获取exec.Command输出的输出.从该输出中,我想获取我需要的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从输出中,我只需要带有requestStatus Failed部分的json数据.剩余的json数据应在每次更新时覆盖/可以删除.您能否建议我如何获取仅需要的数据?源代码:我的源代码看起来像这样.

Here from output I want only json data with requestStatus Failed part only. remaining json data should be override each update/can be deleted. could you pls suggest me how can I get data which is only required. source Code: my source code looks like this.

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.Unmarshal() 解组包含多个(独立)JSON值的内容,例如您的输出(是多个JSON对象的串联).

You can't use json.Unmarshal() to unmarshal something that contains multiple (independent) JSON values, such as your output (which is a concatenation of multiple JSON objects).

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

Use json.Decoder to decode multiple JSON values (objects) from a stream one-by-one.

例如:

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

这将输出(在游乐场上尝试):

This will output (try it on the 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() :

To decode content from your stdBuffer, you may pass that to json.NewDecoder():

dec := json.NewDecoder(&stdBuffer)

如果只需要输出状态为失败" 的对象,只需使用 if 语句:

If you only need to output the object with "failed" status, simply use an if statement:

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

这只会输出(在游乐场上尝试):

This will only output (try it on the 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]

这篇关于我以以下方式获取exec.Command输出的输出.从该输出中,我想获取我需要的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆