gpt4 book ai didi

json - 接收 Json API 响应并使用 Golang 发送 Json 响应

转载 作者:行者123 更新时间:2023-12-01 22:38:47 25 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

2年前关闭。




Improve this question




我正在尝试构建一个简单的 Web 应用程序,当我们转到 url http://localhost:8888/ 时,它应该调用一个 API 端点(其中包含 JSON 响应)并仅选择该 JSON 中的特定关键元素并将该特定关键元素作为 JSON 格式的响应发送。

前往代码:
我正在尝试这个

func viewdags(w http.ResponseWriter, r *http.Request){
response, err := http.Get("http://localhost:8080/admin/rest_api/api?api=list_dags") //API endpoint
if err != nil{
fmt.Printf("HTTP request failed with error %s\n", err)
} else {
//logic goes here


func main(){
router := mux.NewRouter()
router.HandleFunc("/",viewdags)
http.ListenAndServe(":8888",router)


这是 func viewdags(w http.ResponseWriter, r *http.Request) 时的结果 JSON叫做。
{
"airflow_cmd":"airflow list_dags",
"arguments":{
"api":"list_dags"},
"call_time":"Tue, 21 Jan 2020 13:13:20 GMT",
"http_response_code":200,
"output":{
"stderr":"",
"stdin":"",
"stdout":"\n\n-------------------------------------------------------------------\nDAGS\n-------------------------------------------------------------------\nairflow_sample\ndag_today\nexample_bash_operator\nexample_branch_dop_operator_v3\nexample_branch_operator\nexample_http_operator\nexample_passing_params_via_test_command\nexample_pig_operator\nexample_python_operator\nexample_short_circuit_operator\nexample_skip_dag\nexample_subdag_operator\nexample_subdag_operator.section-1\nexample_subdag_operator.section-2\nexample_trigger_controller_dag\nexample_trigger_target_dag\nexample_xcom\nlatest_only\nlatest_only_with_trigger\nmy_custom_dag\nsample_dag\nsample_dag1\ntest_utils\ntutorial\n\n"},
"post_arguments":{},
"response_time":"Tue, 21 Jan 2020 15:00:00 GMT",
"status":"OK"
}

所以,我只想拿 stdout里面是 output并将其作为 JSON 格式的响应传递。
也就是说,JSON 响应应该是:
{
"stdout":"\n\n-------------------------------------------------------------------\nDAGS\n-------------------------------------------------------------------\nairflow_sample\ndag_today\nexample_bash_operator\nexample_branch_dop_operator_v3\nexample_branch_operator\nexample_http_operator\nexample_passing_params_via_test_command\nexample_pig_operator\nexample_python_operator\nexample_short_circuit_operator\nexample_skip_dag\nexample_subdag_operator\nexample_subdag_operator.section-1\nexample_subdag_operator.section-2\nexample_trigger_controller_dag\nexample_trigger_target_dag\nexample_xcom\nlatest_only\nlatest_only_with_trigger\nmy_custom_dag\nsample_dag\nsample_dag1\ntest_utils\ntutorial\n\n"
}

最佳答案

将复杂的 JSON 解码为仅包含所需字段的简单 Go 结构是合法的。输入流中的其他字段将被忽略。

this playground code (改编自 this sample ),JSON 输入

        {
"FirstName":"Napoléon",
"Age": 51,
"Other": null,
"unrelated": 42,
"Things": "foobar"
}

变成了这个更简单的 Go 对象,它有 2 个字段:
{Napoléon 51}

不过要注意的一件事是您的输入字段名称 sdtout是小写的,但你的 Go 结构字段需要大写 Stdout .因此你的类型 def 看起来像
type MyInput struct {
Stdout string `json:"stdout"`
}

关于json - 接收 Json API 响应并使用 Golang 发送 Json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59839147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com