2

I am having one issue while reading the json array. Need help for below query.

Request Json :

{ "httpReq": {
"username": "1234567890",
"password": "1234567890",
"number": "123456"
}
}

Response Json :

{ "httpResp": {
    "status": "Pass",
    "message": "great"
    }
    }

Below was my code: If i am passing the json object below its working, but i need to send "httpReq" in json.

package main

import (
    "encoding/json"
    "fmt"
)


type people struct {
    Username string `json:"username"`
    Password string `json:"password"`
    Number string `json:"number"`
    
}

type peopleread struct {
    Collection []people
}

func main() {
    text := `{
    "username": "1234567890",
    "password": "1234567890",
    "number": "123456"
    
}`
    textBytes := []byte(text)

    //people1 := people{}
    var people2 people
    err := json.Unmarshal(textBytes, &people2)
    if err != nil {
        fmt.Println(err)
        return
    }
    Username := people2.Username
    Password := people2.Password
    Number := people2.Number
        fmt.Println(Username)
        fmt.Println(Password)
        fmt.Println(Number) 
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Quick learner
  • 699
  • 4
  • 22
  • 39
  • 3
    Look at the contents of `textBytes` and compare to your original text, you're not handling the `httpReq` field which contains the data you care about. – Marc May 15 '20 at 12:51
  • your struct is not right, if you wanted to unmarshal the type of json in your request, you would need a struct of type `struct{HttpReq people\`json:"httpReq"\`}`and use that in unmarshalling `json.Unmarshal` – whitespace May 15 '20 at 13:21
  • [The language is called Go](https://www.reddit.com/r/golang/comments/30wsrs/the_name_of_our_language_is_go/). Not GOlang, not Go Lang, not golang. Just Go. – Jonathan Hall Nov 06 '20 at 13:32

1 Answers1

1

To unmarshal with httpReq field you have to handle this.

Create a struct to your wrap your request body like json

type HttpReq struct{
   HttpReq people `json:"httpReq"`
}

Then use unmarshal

var httpReq HttpReq
err := json.Unmarshal(textBytes, &httpReq)

Full code in Go playground here

Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • Could you help me in sending the response json also. { "httpResp": { "status": "Pass", "message": "great" } } – Quick learner May 18 '20 at 06:15
  • type Response struct { Success string `json:"success"` Message string `json:"message"` } type HTTPResp struct { Userresp []Response `json:"httpResp"` } respData := HTTPResp{} str := Reponse{"Success", "Great!"} respData.HTTPResp = append(respData.HTTPResp,str) b, _ := json.Marshal(respData) respbody := string(b) c.String(http.StatusOK, respbody) – Quick learner May 18 '20 at 06:36
  • I am getting this error "first argument to append must be slice; have struct { Sucess string "json:\"success\""; Message string "json:\"message\"" }" – Quick learner May 18 '20 at 06:40
  • Don't use slice `[]Response`, use `Response` in `HTTPResp` struct and set as `respData.Userresp = str` – Eklavya May 18 '20 at 06:50
  • What do you mean by update. I have already accepted your answer – Quick learner May 18 '20 at 07:52
  • I can't upvote the question as it was raised by me :(. Please guide me how to do that – Quick learner May 18 '20 at 08:10
  • Please read here https://stackoverflow.com/help/someone-answers and here https://meta.stackexchange.com/questions/173399/how-can-i-upvote-answers-and-comments – Eklavya May 18 '20 at 09:06