K
K
Kuzma Korsakov2016-02-02 14:54:02
go
Kuzma Korsakov, 2016-02-02 14:54:02

How to parse JSON response from VK.api?

Some wrong JSON from VK ((tell me how to parse

https://api.vk.com/method/wall.get?domain=selengmo...
using Golang?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2016-02-02
@allneva

It will be most convenient to parse in this way.

spoiler
package main

import (
  "encoding/json"
  "log"
)

type RawResponse struct {
  Response []json.RawMessage `json:"response"`
}

type Response struct {
  Id          int          `json:"id"`
  FromId      int          `json:"from_id"`
  ToId        int          `json:"to_id"`
  Date        int          `json:"date"`
  PostType    string       `json:"post_type"`
  Text        string       `json:"text"`
  SignerId    int          `json:"signer_id"`
  IsPinned    int8         `json:"is_pinned"`
  Attachment  Attachment   `json:"attachment"`
  Attachments []Attachment `json:"attachments"`
}

type Attachment struct {
  Type  string `json:"type"`
  Photo *Photo `json:"photo"`
  Link  *Link  `json:"link"`
}

type Photo struct {
  Pid        int    `json:"pid"`
  Aid        int    `json:"aid"`
  OwnerId    int    `json:"owner_id"`
  UserId     int    `json:"user_id"`
  Src        string `json:"src"`
  SrcBig     string `json:"src_big"`
  SrcSmall   string `json:"src_small"`
  SrcXbig    string `json:"src_xbig"`
  SrcXxbig   string `json:"src_xxbig"`
  Width      int    `json:"width"`
  Height     int    `json:"height"`
  Text       string `json:"text"`
  Created    int    `json:"created"`
  Access_key string `json:"access_key"`
}

type Link struct {
  Url         string `json:"url"`
  Title       string `json:"title"`
  Description string `json:"description"`
  ImageSrc    string `json:"image_src"`
  ImageBig    string `json:"image_big"`
}

func main() {
  raw := []byte(`{"response":[2468,{"id":5513,"from_id":-58014516,"to_id":-58014516,"date":1453516206,"post_type":"post","text":"Мир вам и ближним вашим!<br><br>Оправдываться - грех. Прп. авва Исайя поучал: \"Венец добродетелей - любовь; венец страстей - оправдание грехов своих\". Вместо оправданий будем говорить - \"прости\".<br><br>Братия Троицкой Селенгинской обители","signer_id":235194773,"is_pinned":1,"attachment":{"type":"photo","photo":{"pid":404037784,"aid":-7,"owner_id":-58014516,"user_id":100,"src":"http:\/\/cs631329.vk.me\/v631329773\/eefe\/8qL1uNcH-Kg.jpg","src_big":"http:\/\/cs631329.vk.me\/v631329773\/eeff\/IvmP9O8aFLc.jpg","src_small":"http:\/\/cs631329.vk.me\/v631329773\/eefd\/B6ADu5ntll0.jpg","src_xbig":"http:\/\/cs631329.vk.me\/v631329773\/ef00\/bfMgaBRpBcI.jpg","src_xxbig":"http:\/\/cs631329.vk.me\/v631329773\/ef01\/qcrVhaq--Ic.jpg","width":1000,"height":666,"text":"","created":1453470652,"access_key":"053e6253631cae5038"}},"attachments":[{"type":"photo","photo":{"pid":404037784,"aid":-7,"owner_id":-58014516,"user_id":100,"src":"http:\/\/cs631329.vk.me\/v631329773\/eefe\/8qL1uNcH-Kg.jpg","src_big":"http:\/\/cs631329.vk.me\/v631329773\/eeff\/IvmP9O8aFLc.jpg","src_small":"http:\/\/cs631329.vk.me\/v631329773\/eefd\/B6ADu5ntll0.jpg","src_xbig":"http:\/\/cs631329.vk.me\/v631329773\/ef00\/bfMgaBRpBcI.jpg","src_xxbig":"http:\/\/cs631329.vk.me\/v631329773\/ef01\/qcrVhaq--Ic.jpg","width":1000,"height":666,"text":"","created":1453470652,"access_key":"053e6253631cae5038"}},{"type":"link","link":{"url":"http:\/\/selenginskii-monastery.cerkov.ru\/sms-rassylka-monastyrya-pouchenie-dnya\/pouchenie-dnya-373\/","title":" » Поучение дня","description":"","image_src":"http:\/\/cs631418.vk.me\/v631418773\/c773\/OMWEVcI7cno.jpg","image_big":"http:\/\/cs631418.vk.me\/v631418773\/c775\/PbCrn5A8iuA.jpg"}}],"comments":{"count":0},"likes":{"count":77},"reposts":{"count":9}}]}`)

  var raw_res RawResponse
  err := json.Unmarshal(raw, &raw_res)
  if err != nil {
    log.Fatal("Error parsing json: ", err)
  }

  var res Response
  err = json.Unmarshal(raw_res.Response[1], &res)
  if err != nil {
    log.Fatal("Error parsing json: ", err)
  }

  log.Printf("%+v", res)
  log.Println(res.Attachments[0].Photo.Pid)
  log.Println(res.Attachments[1].Link.Title)
}

We solve the first array problem with json.RawMessage, and the attachments problem with pointers to structures. If the field is absent in JSON, the pointer will be equal to nil.

F
fastpars, 2016-02-02
@fastpars

json2struct.mervine.net
mholt.github.io/json-to-go

type MyJsonName struct {
  Attachment struct {
    Photo struct {
      AccessKey string `json:"access_key"`
      Aid       int    `json:"aid"`
      Created   int    `json:"created"`
      Height    int    `json:"height"`
      OwnerID   int    `json:"owner_id"`
      Pid       int    `json:"pid"`
      Src       string `json:"src"`
      SrcBig    string `json:"src_big"`
      SrcSmall  string `json:"src_small"`
      SrcXbig   string `json:"src_xbig"`
      SrcXxbig  string `json:"src_xxbig"`
      Text      string `json:"text"`
      UserID    int    `json:"user_id"`
      Width     int    `json:"width"`
    } `json:"photo"`
    Type string `json:"type"`
  } `json:"attachment"`
  Attachments []struct {
    Photo struct {
      AccessKey string `json:"access_key"`
      Aid       int    `json:"aid"`
      Created   int    `json:"created"`
      Height    int    `json:"height"`
      OwnerID   int    `json:"owner_id"`
      Pid       int    `json:"pid"`
      Src       string `json:"src"`
      SrcBig    string `json:"src_big"`
      SrcSmall  string `json:"src_small"`
      SrcXbig   string `json:"src_xbig"`
      SrcXxbig  string `json:"src_xxbig"`
      Text      string `json:"text"`
      UserID    int    `json:"user_id"`
      Width     int    `json:"width"`
    } `json:"photo"`
    Type string `json:"type"`
  } `json:"attachments"`
  Comments struct {
    Count int `json:"count"`
  } `json:"comments"`
  Date     int `json:"date"`
  FromID   int `json:"from_id"`
  ID       int `json:"id"`
  IsPinned int `json:"is_pinned"`
  Likes    struct {
    Count int `json:"count"`
  } `json:"likes"`
  PostType string `json:"post_type"`
  Reposts  struct {
    Count int `json:"count"`
  } `json:"reposts"`
  SignerID int    `json:"signer_id"`
  Text     string `json:"text"`
  ToID     int    `json:"to_id"`
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question