1. Check the error result code from json.Unmarshal.  It will tell you when 
something is wrong.
2. However in your case, it's much simpler. The JSON contains an element  
{"content": ... }  but your type Rs doesn't have a "Content" member.  
Therefore, it doesn't match any element in the incoming JSON.  Unexpected 
members are ignored, so you're left with the zero value of Rs.

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

On Sunday, 3 October 2021 at 19:25:13 UTC+1 trivedi...@gmail.com wrote:

> Hi all, I am using json.Unamarshal() api to get back the response values 
> sent as a response by the remote API.
> But I am unable to get the response values. 
> When printed the output it displayed as
> {0   }
> But the same response when converted to string, I could see all the values 
> in it.
>
> Few golang statements made use in the client side are
>
> type Rs struct {
>         Id int
>         Name string
>         Email string
>         Phone string
> }
>
> var rs Rs
>  body, err := ioutil.ReadAll(resp.Body)
>                 if err != nil {
>                         log.Fatalln(err)
>                 }
> json.Unmarshal([]byte(body),&rs)
> fmt.Println(rs)
> and the output I got is  {0   }
>
> But when I converted the response to the string format I could see the 
> response as below
> sb := string(body)
>  fmt.Printf(sb)
>
> {"content":"{\"id\":1,\"name\":\"My 
> Name\",\"email\":\"myn...@myname.com\",\"phone\":\"123456789\"}\n"}2021/10/03 
> 23:34:35 {"content":"{\"id\":1,\"name\":\"My Name\",\"email\":\"
> myn...@myname.com\",\"phone\":\"123456789\"}\n"}
>
> Plz let me know what could be the problem.
>
> Thanks
> Nagaraj Trivedi
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e45575a7-1be3-4b29-a919-28a032eca93cn%40googlegroups.com.

Reply via email to