简体   繁体   English

格式良好的 JSON 文件

[英]Well Formed JSON file

I am attempting to set up a local json file that I can use to mock some data for axios for a vuejs project i am working on.我正在尝试设置一个本地 json 文件,我可以用它来模拟我正在处理的 vuejs 项目的 axios 的一些数据。

Following this guide https://medium.com/@negarjf/how-to-access-a-static-json-file-in-vue-cli-3-8943dc343f95遵循本指南https://medium.com/@negarjf/how-to-access-a-static-json-file-in-vue-cli-3-8943dc343f95

everything seems to work except my json file is returning the data as one big string?除了我的 json 文件将数据作为一个大字符串返回之外,一切似乎都有效?

My JSON file looks like this....我的 JSON 文件看起来像这样....

[{label: "Assamese", count: 13},
{label: "Bengali", count: 83},
{label: "Bodo", count: 1.4},
{label: "Dogri", count: 2.3},
{label: "Gujarati", count: 46},
{label: "Hindi", count: 300},
{label: "Kannada", count: 38},
{label: "Kashmiri", count: 5.5},
{label: "Konkani", count: 5},
{label: "Maithili", count: 20},
{label: "Malayalam", count: 33},
{label: "Manipuri", count: 1.5},
{label: "Marathi", count: 72},
{label: "Nepali", count: 2.9},
{label: "Oriya", count: 33},
{label: "Punjabi", count: 29},
{label: "Sanskrit", count: 0.01},
{label: "Santhali", count: 6.5},
{label: "Sindhi", count: 2.5},
{label: "Tamil", count: 61},
{label: "Telugu", count: 74},
{label: "Urdu", count: 52}]

however my console.log is showing me JSON that looks like this...但是我的 console.log 向我展示了看起来像这样的 JSON ......

{label: "Assamese", count: 13},
{label: "Bengali", count: 83},
{label: "Bodo", count: 1.4},
{label: "Dogri", count: 2.3},
{label: "Gujarati", count: 46},
{label: "Hindi", count: 300},
{label: "Kannada", count: 38},
{label: "Kashmiri", count: 5.5},
{label: "Konkani", count: 5},
{label: "Maithili", count: 20},
{label: "Malayalam", count: 33},
{label: "Manipuri", count: 1.5},
{label: "Marathi", count: 72},
{label: "Nepali", count: 2.9},
{label: "Oriya", count: 33},
{label: "Punjabi", count: 29},
{label: "Sanskrit", count: 0.01},
{label: "Santhali", count: 6.5},
{label: "Sindhi", count: 2.5},
{label: "Tamil", count: 61},
{label: "Telugu", count: 74},
{label: "Urdu", count: 52}

As you can see my data is being treated as a string instead of well formed json.如您所见,我的数据被视为字符串而不是格式良好的 json。

Any idea why?知道为什么吗?

UPDATE As requested, this is the axios code I run in my vue file更新根据要求,这是我在 vue 文件中运行的 axios 代码

let dataset = [];
        console.log(this.baseUrl);
        axios.get(this.baseUrl + '/mockdata/pie-chart-data.json').then(response => {
            dataset = response.data;
            console.log(response.data);
        })
            .catch(e => {
                console.log('axios error', e)
            });

Update 2 I updated my response.data更新 2我更新了我的 response.data

It's literally just giving me the json as a string, im not seeing it as a json tree in the firefox console.它实际上只是将 json 作为字符串提供给我,我没有在 firefox 控制台中将其视为 json 树。

Looks like you are making an http request to fetch the .json file and then you are logging the entire response that you are getting back to the console.看起来您正在发出一个 http 请求来获取 .json 文件,然后您正在记录返回到控制台的整个响应。 So the 'data' field is the actual file contents, and the entire object you are logging to console is the whole http request being made.所以“数据”字段是实际的文件内容,您记录到控制台的整个对象是正在发出的整个 http 请求。

[Update] Since you are now logging response.data which is a string, parse the string into an object like so: [更新] 由于您现在记录的 response.data 是一个字符串,因此将字符串解析为一个对象,如下所示:

var json = JSON.parse(response.data);

I think you need console data from response.我认为您需要来自响应的控制台data

For example:例如:

let res = await axios.get(
    'https://...'
  );
 console.log(res.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM