douyaosi3164 2018-12-01 21:56
浏览 63
已采纳

如何解析Golang中正在处理的JSON文件

I have a file with JSON in this format:

[{
    "id": "1055972353245622272",
    "lang": "und",
    "date": "Sat Oct 27 00:00:02 +0000 2018",
    "text": "#BTC 6474 346 0 08 #ETH 203 317 0 13 #XRP 0 459 0 04 #BCH 438 922 0 0 #EOS 5 388 0 12 #XLM 0 235 0 41 #LTC 52 106 0 03 #ADA 0 074 0 17 #USDT 0 99 0 07 #XMR 105 022 0 13 #TRX 0 024 0 21 "
},
{
    "id": "1055972355506401280",
    "lang": "en",
    "date": "Sat Oct 27 00:00:03 +0000 2018",
    "text": "Don t want to miss any of our public #crypto trading #signals Want instant updates of our premium channel #performance Searching for #crypto news Get instantly notified on our public telegram channel Join now at https t co akfmLiArya #DGB #SC #MFT #EOS #XVG #BTC #TRX https t co HT2RAOIjfh"
},

This file is being worked on by program1 in random intervals (when a tweet that match filter is found). I want to read this file by program2 - in 5 minutes timesteps. But I am unable to.

The unmarshaling (json.Unmarshal(file, &data)) is not allowing me to read it - as it throws an error as JSON is not correct.

I do not want to redesign the architecture with the usage of DB, I want to be able to operate on files as intended.

How can I access the files and have them parsed as JSON?

EDIT1: workaround with reading the file and closing the JSON

file, _ := ioutil.ReadFile(fileName)
closingJson := "{}]"
file = append(file, closingJson...)
json.Unmarshal(file, &data)
  • 写回答

1条回答 默认 最新

  • doulao3905 2018-12-01 22:33
    关注

    You just need to treat it as a JSON stream:

    https://play.golang.org/p/6drcizYKrrJ

        type Message struct {
          Id   string `json:"id"`
          Lang string `json:"lang"`
          Date string `json:"date"`
          Text string `json:"text"`
        }
    
        jsonStream, err := os.Open(`/tmp/json`)
        if err != nil {
            panic(err)
        }
    
        dec := json.NewDecoder(jsonStream)
    
        // read open bracket
        _, err := dec.Token()
        if err != nil {
          log.Fatal(err)
        }
    
        // while the array contains values
        for dec.More() {
          var m Message
          // decode an array value (Message)
          err := dec.Decode(&m)
          if err == nil {
            fmt.Printf("%v : %v : %v : %v
    ", m.Id, m.Lang, m.Date, m.Text)
          } else {
            // wait for more contents - sleep?  use a channel and wait to be notified?
          }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog