简体   繁体   English

保存JSON文件时出错

[英]Error saving JSON file

I have following JSON file: 我有以下JSON文件:

weather = [ 
   {      
         "city": "Munich",
         "temp": {
              "temp_val": "30 deg",
              "temp_unit": "C"
          },
         "speed": {
              "speed_val": 7.31,
              "speed_unit": "m/s"
          }
   }
]

I am new to working with JSON files. 我是使用JSON文件的新手。 I want to save this JSON file as weather.json. 我想将此JSON文件另存为weather.json。

But it gives following error: 但是它给出了以下错误:

Expected value at 1:0 pointing to the first line of the file. 指向文件第一行的1:0的期望值。

You can't have weather = in your JSON file. 您的JSON文件中不能包含weather = JSON stands for JavaScript Object Notation so anything other than a JavaScript Object won't work. JSON表示JavaScript对象表示法,因此除JavaScript对象外,其他所有功能均不起作用。 You also can't have functions in there. 您也不能在其中拥有功能。 Take a look at the JSON official website to see what the format accepts 查看JSON官方网站以了解该格式可以接受的格式

This should be correct: 这应该是正确的:

[ 
   {      
         "city": "Munich",
         "temp": {
              "temp_val": "30 deg",
              "temp_unit": "C"
          },
         "speed": {
              "speed_val": 7.31,
              "speed_unit": "m/s"
          }
   }
]

You can then add this line in your javascript once you load the file into a string: 将文件加载到字符串中后,即可在JavaScript中添加以下行:

weather = JSON.parse(some_string);

You are manipulating this file as if it was going to be rendered as JavaScript. 您正在处理此文件,就像它将被呈现为JavaScript一样。 This should only be plain text, no variable definitions. 这只能是纯文本,不能是变量定义。 Just plain key value pairs. 只是简单的键值对。 If you want to assign variable, set it to a .js file and render it in a browser. 如果要分配变量,请将其设置为.js文件并在浏览器中呈现。

You should look at an example file for JSON and model it. 您应该查看JSON的示例文件并对其进行建模。 Remember that JSON is just plain text organized in a particular way. 请记住,JSON只是以特定方式组织的纯文本。

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

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