Answer the question
In order to leave comments, you need to log in
How to parse json containing both numbers and an object in a sheet?
Hello, I'm learning Golang, I can't figure out how to designate this type of json:
{
"response":[
370,
{
"id":616,
"from_id":-101299645,
"to_id":-101299645,
"date":1497367444,
"marked_as_ads":0,
"post_type":"post",
"text":"прайс актуален НО <br>АКЦИЯ \"знакомство с мастером\" - первое посещение любой дизайн бесплатно!! Спешите записаться, это будет не всегда))",
"can_delete":1,
"can_pin":1,
"is_pinned":1,
"attachment":{
"type":"photo",
"photo":{
"pid":456239098,
"aid":-7,
"owner_id":-101299645,
"user_id":100,
"src":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c6f\/m_XTc7E9STE.jpg",
"src_big":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c70\/Jv0k8qYu1BU.jpg",
"src_small":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c6e\/ht4z4oVe-Jo.jpg",
"src_xbig":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c71\/0zpqUJ9BOBY.jpg",
"src_xxbig":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c72\/FQa5iNg2NnU.jpg",
"width":763,
"height":1080,
"text":"",
"created":1497367446,
"post_id":616,
"access_key":"9a6b6d3977113f56d0"
}
},
"attachments":[
{
"type":"photo",
"photo":{
"pid":456239098,
"aid":-7,
"owner_id":-101299645,
"user_id":100,
"src":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c6f\/m_XTc7E9STE.jpg",
"src_big":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c70\/Jv0k8qYu1BU.jpg",
"src_small":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c6e\/ht4z4oVe-Jo.jpg",
"src_xbig":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c71\/0zpqUJ9BOBY.jpg",
"src_xxbig":"https:\/\/pp.userapi.com\/c637228\/v637228340\/56c72\/FQa5iNg2NnU.jpg",
"width":763,
"height":1080,
"text":"",
"created":1497367446,
"post_id":616,
"access_key":"9a6b6d3977113f56d0"
}
}
],
"comments":{
"count":0
},
"likes":{
"count":1
},
"reposts":{
"count":1
}
}
]
}
type vkResponseWallItem struct {
id int `json:"id"`
from_id int `json:"from_id"`
owner_id int `json:"owner_id"`
date int `json:"date"`
marked_as_ads int `json:"marked_as_ads"`
post_type string `json:"post_type"`
text string `json:"string"`
attachments []string `json:"attachments"`
post_source map[string]string `json:"post_source"`
comments map[string]int `json:"comments"`
likes map[string]int `json:"likes"`
reposts map[string]int `json:"reposts"`
}
res := map[string][]interface{}{int{}, vkResponseWallItem{}}
json.Unmarshal([]byte(strResp), &res)
log.Println(res["response"][1]["text"])
Answer the question
In order to leave comments, you need to log in
Firstly, when you name a structure field with a small letter, it means that the field is private and it will not participate in json parsing. Therefore, name such fields with a capital letter.
If you know that the first element is always a number and the second is an object, then you can use json.RawMessage to parse the nested structures later.
https://play.golang.org/p/BvW3TIT52N
package main
import (
"encoding/json"
"fmt"
"log"
)
type Attachment struct {
Type string `json:"type"`
}
type CountOnly struct {
Count int `json:"count"`
}
type vkResponseWallItem struct {
Id int `json:"id"`
FromId int `json:"from_id"`
OwnerId int `json:"owner_id"`
Date int `json:"date"`
MarkedAsAds int `json:"marked_as_ads"`
PostType string `json:"post_type"`
Text string `json:"text"`
Attachment Attachment `json:"attachment"`
Attachments []Attachment `json:"attachments"`
Comments CountOnly `json:"comments"`
Likes CountOnly `json:"likes"`
Reposts CountOnly `json:"reposts"`
}
type Response struct {
Response []json.RawMessage `json:"response"`
}
func main() {
data := []byte(`{"response":[370,{"id":616,"from_id":-101299645,"to_id":-101299645,"date":1497367444,"marked_as_ads":0,"post_type":"post","text":"прайс актуален НО <br>АКЦИЯ \"знакомство с мастером\" - первое посещение любой дизайн бесплатно!! Спешите записаться, это будет не всегда))","can_delete":1,"can_pin":1,"is_pinned":1,"attachment":{"type":"photo","photo":{"pid":456239098,"aid":-7,"owner_id":-101299645,"user_id":100,"src":"https://pp.userapi.com/c637228/v637228340/56c6f/m_XTc7E9STE.jpg","src_big":"https://pp.userapi.com/c637228/v637228340/56c70/Jv0k8qYu1BU.jpg","src_small":"https://pp.userapi.com/c637228/v637228340/56c6e/ht4z4oVe-Jo.jpg","src_xbig":"https://pp.userapi.com/c637228/v637228340/56c71/0zpqUJ9BOBY.jpg","src_xxbig":"https://pp.userapi.com/c637228/v637228340/56c72/FQa5iNg2NnU.jpg","width":763,"height":1080,"text":"","created":1497367446,"post_id":616,"access_key":"9a6b6d3977113f56d0"}},"attachments":[{"type":"photo","photo":{"pid":456239098,"aid":-7,"owner_id":-101299645,"user_id":100,"src":"https://pp.userapi.com/c637228/v637228340/56c6f/m_XTc7E9STE.jpg","src_big":"https://pp.userapi.com/c637228/v637228340/56c70/Jv0k8qYu1BU.jpg","src_small":"https://pp.userapi.com/c637228/v637228340/56c6e/ht4z4oVe-Jo.jpg","src_xbig":"https://pp.userapi.com/c637228/v637228340/56c71/0zpqUJ9BOBY.jpg","src_xxbig":"https://pp.userapi.com/c637228/v637228340/56c72/FQa5iNg2NnU.jpg","width":763,"height":1080,"text":"","created":1497367446,"post_id":616,"access_key":"9a6b6d3977113f56d0"}}],"comments":{"count":0},"likes":{"count":1},"reposts":{"count":1}}]}`)
var response Response
err := json.Unmarshal(data, &response)
if err != nil {
log.Fatal(err)
}
var number int
err = json.Unmarshal(response.Response[0], &number)
if err != nil {
log.Fatal(err)
}
var wallItem vkResponseWallItem
err = json.Unmarshal(response.Response[1], &wallItem)
if err != nil {
log.Fatal(err)
}
fmt.Println("num:", number)
fmt.Println("wall item:", wallItem)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question