使用 Golang 的 JSON 响应示例

     2023-02-25     59

关键词:

【中文标题】使用 Golang 的 JSON 响应示例【英文标题】:Example JSON response with Golang 【发布时间】:2019-11-26 00:41:48 【问题描述】:

我正在尝试使用 golang 构建 API。首先,我只是在访问http://localhost:8085/search 时尝试发送一些 json 数据,但我在浏览器中查看的只是null

我从Medium post得到这个例子

package main

import (
    "log"
    "net/http"
    "encoding/json"
    "github.com/gorilla/mux"
)

type Place struct 
  Location string `json:"123 Houston st"`
  Name string `json:"Ricks Barber Shop"`
  Body string `json:"this is the best barber shop in the world"`


var place []Place

func search(write http.ResponseWriter, req *http.Request) 
    write.Header().Set("Content-Type", "application/json")
    json.NewEncoder(write).Encode(place)


func main() 
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/search", search).Methods("GET")
    log.Fatal(http.ListenAndServe(":8085", router))

【问题讨论】:

【参考方案1】:

您的“地点”变量没有赋值。我想您正在尝试通过 json 标签分配值,但是此标签用于通知 json 文件中 json 属性的名称,而不是属性的值。

将您的代码调整为以下内容,它应该可以工作

type Place struct 
  Location string `json:"location"`
  Name string `json:"name"`
  Body string `json:"body"`


var place []Place

func search(write http.ResponseWriter, req *http.Request) 

  place = append(place, PlaceLocation: `123 Houston st`, Name:`Ricks Barber Shop`, Body:`this is the best barber shop in the world`)

   write.Header().Set("Content-Type", "application/json")
   j, err := json.Marshal(&place)

   if err != nil 
        //Your logic to handle Error
       

   fmt.Fprint(write, string(j)


Working command line program。您可以根据自己的需要进行调整。

https://play.golang.org/p/yHTcbqjoCjx

【讨论】:

使用 Golang 重构 Json

】使用Golang重构Json【英文标题】:RestructuringJsonusingGolang【发布时间】:2017-07-0300:52:00【问题描述】:我有以下JSON响应:["talent_id":"b520ad50-5302-45ce-9121-5ff42d67b4fb","platform":"facebook","posts":["insights":["name":"post_impressions_organic 查看详情

iOS和使用Json进行请求/响应的消费WCF服务方法

】iOS和使用Json进行请求/响应的消费WCF服务方法【英文标题】:iOSandConsumingWCFservicemethodusingJsonforRequest/Response【发布时间】:2011-12-2304:09:30【问题描述】:我在Internet上看到了各种使用json使用WCF服务的示例,但是还没有看到使用iO... 查看详情

Golang在json响应中获取数组索引值

】Golang在json响应中获取数组索引值【英文标题】:Golanggetarrayindexvalueinjsonresponse【发布时间】:2016-06-2511:14:06【问题描述】:所以我对数据库(mongodb)进行了一些查询,它将按值字段对结果进行排序。all:=EValueserr:=con.Find(bson.M"na... 查看详情

解析来自 Httpwebrequest 的 Json 响应

...【发布时间】:2020-01-0917:48:03【问题描述】:我正在尝试使用JSON格式的httpwebrequest解析响应中的数据,我尝试使用JSON.Net但似乎没有运气,如果需要,我愿意使用Regex。当前代码示例-varrequest=(HttpWebRequest)WebRequest 查看详情

使用 JMeter 比较多个 JSON 响应

】使用JMeter比较多个JSON响应【英文标题】:ComparemultipleJSONresponseswithJMeter【发布时间】:2015-02-2619:04:31【问题描述】:我需要说服JMeter对JSONWeb服务请求响应进行A/B比较。我面临的挑战是响应可能具有相同的内容,但仍然不一样... 查看详情

使用 Laravel 测试 JSON 响应

】使用Laravel测试JSON响应【英文标题】:TestingJSONresponsewithLaravel【发布时间】:2019-02-1910:57:16【问题描述】:我使用Laravel准备了一个示例API。它总是返回这个对象:"a":1,"b":2现在,我想更好地了解如何使用phpunit对其进行测试。我... 查看详情

如何使用 Java 返回部分 JSON 响应?

】如何使用Java返回部分JSON响应?【英文标题】:HowtoreturnapartialJSONresponseusingJava?【发布时间】:2012-03-0801:19:16【问题描述】:我正在构建一个RESTfulAPI,并希望为开发人员提供选择在JSON响应中返回哪些字段的选项。Thisblogpost展示... 查看详情

java示例代码_添加并忽略json响应的字段

java示例代码_添加并忽略json响应的字段 查看详情

json链接帖子的facebook响应对象(示例)(代码片段)

查看详情

java示例代码_在Android中解析复杂的json响应

java示例代码_在Android中解析复杂的json响应 查看详情

Retrofit-2.0 - 解析在 xml 中包含 json 的响应

...hinxml【发布时间】:2016-05-2809:07:47【问题描述】:我正在使用Retrofit2.0,我想解析自定义响应。POST请求包含json格式。而响应是xml和json的组合。示例请求:"loginid":"10051"示例响应:<stringxmlns="http 查看详情

golang在go(golang)中使用http.get的示例(代码片段)

查看详情

java示例代码_解析这个查询字符串,它是JSON响应的结果

java示例代码_解析这个查询字符串,它是JSON响应的结果 查看详情

json这是data.usajobs.govjobsapi的示例空响应。(代码片段)

查看详情

如何处理 JSON 响应

...sponse【发布时间】:2015-02-0313:24:30【问题描述】:我正在使用Pythonwebsockets来返回JSON对象(我认为)但是我不确定如何处理响应。我有一个使用parseJSON的JavaScript示例,下面是一个sn-p:socket=io.connect("__socketaddress__");socket.on("connect" 查看详情

golang的json数据解析

import(    "fmt"    "time"    "github.com/astaxie/beego"    "github.com/bitly/go-simplejson")typeDatasstruct{   查看详情

RAML 1.0 - 单个响应的多个示例

...】:2016-12-2203:01:07【问题描述】:我无法在任何地方找到使用!include演示多个示例的任何代码。我正在尝试以下操作:200:description:Successfulprojectcreationbody:application/json:type:JiraRespons 查看详情

使用 nodeFetch 在 JSON API 响应中正确显示特殊字符

】使用nodeFetch在JSONAPI响应中正确显示特殊字符【英文标题】:CorrectlydisplayspecialcharactersinJSONAPIresponseusingnodeFetch【发布时间】:2019-11-0408:06:43【问题描述】:我有一个通过HTTP与RESTfulAPI通信的反应应用程序。API返回一个可能包含... 查看详情