dongxia8656 2019-01-22 20:46
浏览 27
已采纳

将很大的json解码为结构数组

I have a web application which have a REST API, get JSON as input and perform transformations of this JSON.

Here is my code:

func (a *API) getAssignments(w http.ResponseWriter, r *http.Request) {

   var document DataPacket
   err := json.NewDecoder(r.Body).Decode(&document)
   if err != nil {
       a.handleJSONParseError(err, w)
      return
   }

   // transformations

JSON which I get is a collection of structs. External application use my application and send me very big json files (300-400MB). Decode this json at the one moment of time takes a very big time and amount of memory.

Is there any way to work with this json as stream and decode structs from this collection one by one ?

  • 写回答

1条回答 默认 最新

  • dounianxie2058 2019-01-22 20:50
    关注

    First, read the documentation.


    Package json

    import "encoding/json"

    func (*Decoder) Decode

    func (dec *Decoder) Decode(v interface{}) error
    

    Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.

    Example (Stream): This example uses a Decoder to decode a streaming array of JSON objects.

    Playground: https://play.golang.org/p/o6hD-UV85SZ

    package main
    
    import (
        "encoding/json"
        "fmt"
        "log"
        "strings"
    )
    
    func main() {
        const jsonStream = `
        [
            {"Name": "Ed", "Text": "Knock knock."},
            {"Name": "Sam", "Text": "Who's there?"},
            {"Name": "Ed", "Text": "Go fmt."},
            {"Name": "Sam", "Text": "Go fmt who?"},
            {"Name": "Ed", "Text": "Go fmt yourself!"}
        ]
    `
        type Message struct {
            Name, Text string
        }
        dec := json.NewDecoder(strings.NewReader(jsonStream))
    
        // read open bracket
        t, err := dec.Token()
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%T: %v
    ", t, t)
    
        // while the array contains values
        for dec.More() {
            var m Message
            // decode an array value (Message)
            err := dec.Decode(&m)
            if err != nil {
                log.Fatal(err)
            }
    
            fmt.Printf("%v: %v
    ", m.Name, m.Text)
        }
    
        // read closing bracket
        t, err = dec.Token()
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%T: %v
    ", t, t)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制