登录
首页 >  Golang >  Go问答

使用 Golang 解析 JSON API 响应和构建 JSON 响应

来源:stackoverflow

时间:2024-03-15 22:15:29 105浏览 收藏

使用 Go 语言解析 JSON API 响应并构建 JSON 响应时,可将复杂的 JSON 解组为仅包含所需字段的简单 Go 结构。输入流中的其他字段将被忽略。此方法允许轻松提取特定数据并将其作为 JSON 格式的响应发送,从而简化 API 交互和数据处理。

问题内容

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

go 代码: 我正在尝试这个

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

因此,我只想获取 output 内的 stdout 并将其作为 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"
        }

变成了这个更简单的具有 2 个字段的 go 对象:

{napoléon 51}

需要注意的一件事是,您的输入字段名称 sdtout 是小写的,但您的 go 结构体字段需要大写 stdout。因此你的 type def 看起来像

type MyInput struct {
    Stdout string `json:"stdout"`
}

终于介绍完啦!小伙伴们,这篇关于《使用 Golang 解析 JSON API 响应和构建 JSON 响应》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>