dongluyi5123 2018-10-30 10:50
浏览 44
已采纳

Golang中的JSON元帅arraylist

Hi there I back to ask again sorry if you guys think I lack of searching but been doing this for hours and I still don't get it why

I have a controller like this in Golang :

c3 := router.CreateNewControllerInstance("index", "/v4")
c3.Post("/insertEmployee", insertEmployee)

and using Postman to throw this arraylist request , the request is like this

[{
  "IDEmployee": "EMP4570787",
  "IDBranch": "ALMST",
  "IDJob": "PRG",
  "name": "Mikey Ivanyushin",
  "street": "1 Bashford Point",
  "phone": "9745288064",
  "join_date": "2008-09-18",
  "status": "fulltime"
},{
  "IDEmployee": "EMP4570787",
  "IDBranch": "ALMST",
  "IDJob": "PRG",
  "name": "Mikey Ivanyushin",
  "street": "1 Bashford Point",
  "phone": "9745288064",
  "join_date": "2008-09-18",
  "status": "fulltime"
}]

and also I have 2 Struct like this

type EmployeeRequest struct {
    IDEmployee string `json: "id_employee"`
    IDBranch   string `json: "id_branch"    `
    IDJob      string `json: "id_job"   `
    Name       string `json: "name" `
    Street     string `json: "street"   `
    Phone      string `json: "phone"    `
    JoinDate   string `json: "join_date"    `
    Status     string `json: "status"   `
    Enabled int `json: "enabled"    `
    Deleted int `json: "deletedstring"`
}

type ListEmployeeRequest struct {
    ListEmployee []EmployeeRequest `json: "request"`
}

for some reason the problem start here , when I try to run my function to read the json that I send from Postman

here is the function that I try to run

func insertEmployee(ctx context.Context) {
    tempReq := services.ListEmployeeRequest{}

    temp1 := ctx.ReadJSON(&tempReq.ListEmployee)

    var err error
    if err = ctx.ReadJSON(temp1); config.FancyHandleError(err) {
        fmt.Println("err readjson ", err.Error())
    }
    parsing, errParsing := json.Marshal(temp1)
        fmt.Println("", string(parsing))
    fmt.Print("", errParsing)
}

the problem occur on the line if err = ctx.ReadJSON(temp1); config.FancyHandleError(err) it tell me that I got unexpected end of JSON input am I doing something wrong there when I throw the request from Postman? or I made a wrong validation code?

can someone tell me what to do next for me to be able to read the JSON that I throw from postman?

thanks for your attention and help , much love <3

  • 写回答

2条回答 默认 最新

  • dso407787736 2018-10-30 11:10
    关注

    You have multiple issues here:

    1. Tag names don't match the JSON input, e.g. id_employee vs IDEmployee.
    2. Your tag syntax is not correct, it shows when you run go vet, it should be json:"id_employee"
    3. You don't need another List struct, if you use it your json should be {"requests":[...]}. Instead you can deserialize a slice:

      var requests []EmployeeRequest
      if err := json.Unmarshal([]byte(j), &requests); err != nil {
          log.Fatal(err)
      }
      

    You can see a running version here: https://play.golang.org/p/HuycpNxE6k8

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里