I'am trying to develop simple chat application. And User talks coming from webservice like this json. And I unmarshaling this json as map[string]interface{}. My question is I am trying to get all "talk_messages" in for loop. But I can't.
{
"talk_id": 0,
"receiver_id": 1,
"receiver_name":"Jack",
"sender_id": 0,
"talk_messages":[
{
"message_id": 0,
"body": "Helooo",
"send_date": "12/3/2017 4:57:15 PM",
"sender_id": 0,
"talk_id": 0
},
{
"message_id": 1,
"body": "Helooo",
"send_date": "12/3/2017 4:58:15 PM",
"sender_id": 1,
"talk_id": 0
},
{
"message_id": 2,
"body": "Whatsapp",
"send_date": "12/3/2017 4:59:22 PM",
"sender_id": 0,
"talk_id": 0
},
{
"message_id": 3,
"body": "Lorem impus",
"send_date": "12/3/2017 5:01:15 PM",
"sender_id": 1,
"talk_id": 0
}
]
}
here is my for loop. What is my problem?
var talkData map[string]interface{}
if unMarshalError := json.Unmarshal([]byte(data), &talkData); unMarshalError != nil {
fmt.Println("Talk initialize error :", unMarshalError)
}
idString := fmt.Sprintf("%v", talkData["talk_id"])
talk.id, _ = strconv.ParseInt(idString, 10, 64)
talk.playerOneId = fmt.Sprintf("%v", talkData["receiver_id"])
talk.playerTwoId = fmt.Sprintf("%v", talkData["sender_id"])
talk.receiverName = fmt.Sprintf("%v", talkData["receiver_name"])
for _, val := range talkData["talk_messages"] {
fmt.Println(val)
}
fmt.Println(talk.id, talk.playerOneId, talk.playerTwoId)