将JSON文件加载到JS对象中 [英] Load a JSON file into a JS object

查看:140
本文介绍了将JSON文件加载到JS对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将json文件加载到JavaScript var中,但它无法解决。

I try to load a json file into a JavaScript var but it just doesn't work out.

var jsonString = JSON.stringify('./test.json');
var obj = JSON.parse(jsonString);

console.log(obj.details.ProductID);

它表示它无法读取未定义的属性ProductID。

it says that it can't read property ProductID of undefined.

我在这里做错了什么?

推荐答案

你需要进行AJAX调用以获取文件。 $。getJSON 就是为了这个目的:

You need to make an AJAX call to get the file. $.getJSON was intended for exactly this purpose:

$.getJSON('./test.json', function(obj) {
    console.log(obj.details.ProductID);
});

这篇关于将JSON文件加载到JS对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆