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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵