【问题标题】:How to extract multiple JSON objects from one json file located outside the app directory in angular?如何从位于应用程序目录之外的一个json文件中提取多个JSON对象?
【发布时间】:2022-06-15 16:16:11
【问题描述】:

我对 Json 文件非常陌生。如果我有一个包含多个 json 对象的 json 文件,位于 /var/log 的 angular 应用程序资产目录之外,则 json 对象如下所示:

{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
 "Code":[{"event1":"A","result":"1"},…]}
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
 "Code":[{"event1":"B","result":"1"},…]}
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
 "Code":[{"event1":"B","result":"0"},…]}

我想将所有“时间戳”和“有用性”提取到 Angular js 中的数据框中,这是我的代码:

import { Component, Input } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
//import json from '../../../../../../../../var/log/suricata/eve.json';
import data from "../../../../../../../../var/log/suricata/eve.json";
import { ClassToggleService, HeaderComponent } from '@coreui/angular';

@Component({
  selector: 'app-default-header',
  templateUrl: './default-header.component.html',
})
export class DefaultHeaderComponent extends HeaderComponent {

  @Input() sidebarId: string = "sidebar";

  public newMessages = new Array(4)
  public newTasks = new Array(5)
  public newNotifications = new Array(5)


  constructor(private classToggler: ClassToggleService) {
    super();
    
    let products: any = (data as any).default;
    const myJSON = JSON.stringify(products);
    var res = JSON.parse('[' + products.replace(/}}}{/g, '}}},{') + ']');
    console.log(res);
    //console.log(data.timestamp);
    //console.log(data.stats.uptime);
  }
}

【问题讨论】:

  • 据我所知,您不允许以这种方式访问​​用户的文件系统。您必须设置本地存储并确保将日志放到那里以便您可以访问它们。
  • 好的,但是我如何读取多个对象,因为它们不是数组的形式?
  • 由于它不兼容 JSON,您必须为此编写自己的解析函数。我会寻找一个正则表达式来为你解决这个问题。提取 JSON 对象,将其转换为 Javascript 对象,然后执行下一步。
  • 任何可能的解决方案?

标签: javascript angular angularjs object parsing


猜你喜欢
  • 2015-03-10
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 2021-08-10
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
相关资源
最近更新 更多