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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?