json.parse在js中工作但不在角度ts中工作

问题描述 投票:0回答:1

我正在将现有的js应用程序重新编写为angular和ts。我有一个json数据文件,我作为现有的js应用程序启动的一部分阅读,它工作正常。当我尝试在完全相同的文件中读取完全相同的文件时,角度,ts /角度barfs完全相同。以下是现有应用的代码:

    aircraftData = JSON.parse(data);

并且控制台日志为空。以下是应用程序的新角度版本的代码:

    const content = require('./shared/imeiXref.json');  
    console.log("IMEI=",content[0].IMEI);

和控制台输出:

AppComponent_Host.ngfactory.js? [sm]:1 ERROR SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at AppComponent.push../src/app/app.component.ts.AppComponent.ngOnInit (app.component.ts:33)
    at checkAndUpdateDirectiveInline (core.js:22089)
    at checkAndUpdateNodeInline (core.js:23353)
    at checkAndUpdateNode (core.js:23315)
    at debugCheckAndUpdateNode (core.js:23949)
    at debugCheckDirectivesFn (core.js:23909)
    at Object.eval [as updateDirectives] (AppComponent_Host.ngfactory.js? [sm]:1)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:23901)
    at checkAndUpdateView (core.js:23297)
View_AppComponent_Host_0 @ AppComponent_Host.ngfactory.js? [sm]:1
proxyClass @ compiler.js:18234
push../node_modules/@angular/core/fesm5/core.js.DebugContext_.logError @ 
core.js:24129
push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ core.js:15762
(anonymous) @ core.js:18116
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ 
zone.js:391
.....

JSON是否应该采用与js到angular / ts不同的格式?

谢谢....

json angular typescript
1个回答
0
投票

它实际上已经在工作了。我没有意识到当我读取文件时,我已经能够通过字段名解析JSON,所以我甚至不需要运行JSON.parse。

© www.soinside.com 2019 - 2024. All rights reserved.