SyntaxError:位于0的JSON中的意外标记T.

时间:2018-01-13 14:10:49

标签: json node.js typescript d3.js

我正在编写一个Typescript应用程序。在这个应用程序中,我用函数d3.json读取了一些json文件,但代码不起作用。错误是这样的:

  

SyntaxError:位置0的JSON中出现意外的标记T.      在JSON.parse()
     在对象。 (d3-request.node.js:176)
     作出回应(d3-request.node.js:39)
     at exports.XMLHttpRequest.onload.xhr.onreadystatechange(d3-request.node.js:30)
     at exports.XMLHttpRequest.dispatchEvent(XMLHttpRequest.js:591)
     at setState(XMLHttpRequest.js:610)
     at exports.XMLHttpRequest.handleError(XMLHttpRequest.js:532)
     在module.exports.errorHandler(XMLHttpRequest.js:459)
     在module.exports.EventEmitter.emit(events.js:81)
     at request.js:142

代码是这样的:

export async function setSelectConfiguration(
filenameJson: string, storeData: (str: string) => void, element: JQuery,
listenerFunction:(e: JQueryEventObject) => void) {
await d3.json("filename2.json", function(error, data) {
    if(error) {
        console.log(error);
    } else {
        console.log(data);
        storeData(data.toString())
    }
});
 element.on("change", listenerFunction);
    }

json是这样的:

{
"files": [
{
        "name": "time_sensitive_test.json",
        "type": "user",
        "owner": "Master",
        "data_type": "linear data",
        "description": "Utenti master (Enrica) periodo 20/05/2014-16/10/2014",
    "ids_description": "accesso risorse",
    "ods_description": "chat/forum",
    "bds_description": "login/logout"
}
]}

gulpfile.js:

var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var tsify = require('tsify');
var sourcemaps = require('gulp-sourcemaps');
var buffer = require('vinyl-buffer');


var paths = {
    pages: ['src/*.html']
};

gulp.task('copyHtml', function () {
    return gulp.src(paths.pages)
        .pipe(gulp.dest('dist'));
   });

   gulp.task('default', ['copyHtml'], function () {
    return browserify({
       basedir: '.',
        debug: true,
        entries: ['src/main.ts'],
       cache: {},
       packageCache: {}
   })
   .plugin(tsify)
    .transform('babelify', {
        presets: ['env','stage-0'],
       extensions: ['.ts'],
        plugins: ['transform-runtime','transform-async-to-generator']
   })
    .bundle()
    .pipe(source('project.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('dist'));
  });

package.json:

    {
  "name": "project",
  "version": "1.0.0",
  "description": " Project",
  "main": "./dist/project.js",
  "directories": {
    "example": "example"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": " ... "
  },
  "author": "  ",
  "license": "ISC",
  "bugs": {
    "url": "... "
  },
  "homepage": "...",
  "devDependencies": {
    "@types/d3": "^4.10.0",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-0": "^6.24.1",
    "babelify": "^8.0.0",
    "browserify": "^14.5.0",
    "gulp": "^3.9.1",
    "gulp-sourcemaps": "^2.6.3",
    "gulp-typescript": "^3.2.3",
    "gulp-uglify": "^3.0.0",
    "gulp-util": "^3.0.8",
    "tsify": "^3.0.4",
    "typescript": "^2.6.2",
    "vinyl-buffer": "^1.0.1",
    "vinyl-source-stream": "^2.0.0",
    "watchify": "^3.9.0"
  },
  "dependencies": {
    "@types/node": "^8.5.7",
    "babel-polyfill": "^6.26.0",
    "babel-runtime": "^6.26.0",
    "d3": "^4.12.2",
    "webpack": "^3.10.0"
  }
}

tsconfig.json:

{
"files": [
  ...
],
"compilerOptions": {
    "module": "commonjs",
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true,
    "noImplicitAny": true,
    "target": "es2015",
    "lib": ["es2015", "es2016", "dom", "es2017", "es6", "es5"],
    "experimentalAsyncFunctions": true,
    "typeRoots": [
        "./node_modules/@types"
    ]
},
"exclude": [
    "node_modules",
    "dist"
]
}

webpack.config.json:

    module.exports = {
    context: __dirname + '/src',
    entry: ['babel-polyfill', './main.ts'],
    output: {
        path: 'dist',
        filename: 'project.js'
    },
    module: {
        loaders: [
            {
                test: /\.js/,
                exclude: /node_modules/,
                loader: 'babel',
                query: {
                    presets: ['es2015']
                }
            }
        ]
    }
};

我运行了在IDE外部编译的代码,但错误同样出现;所以它不应该是IDE的一些奇怪的错误。
我试过用一个纯粹的javascript应用程序读取文件,一切正常;所以问题不在文件中找到。
可能是什么问题呢?例如:它需要任何特殊模块或配置吗?

0 个答案:

没有答案