简体   繁体   English

json文件Javascript的正确格式是什么?

[英]What is the correct format for json file Javascript?

I am trying to test a simple code where I am extracting data from JSON file and displaying an alert. 我正在尝试测试一个简单的代码,该代码用于从JSON文件提取数据并显示警报。 I am confused about the format of the JSON file. 我对JSON文件的格式感到困惑。 The following code works perfectly fine: Working code 以下代码可以正常工作工作代码 在此处输入图片说明 Code: 码:

<!DOCTYPE html>
<html>
<body>
<h2>My First Web Pags</h2>
<p>My first paragraph.</p>
<script> 
var text = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}';
var jsonScript = JSON.parse(text);
var titledata = jsonScript.name; 
alert(titledata);
</script>
</body>
</html> 

On the other hand, when I change the format of the JSON, the alert is not generated. 另一方面,当我更改JSON的格式时,不会生成警报。 Faulty Code 错误的代码 在此处输入图片说明 Code: 码:

<!DOCTYPE html>
<html>
<body>
<h2>My First Web Pags</h2>
<p>My first paragraph.</p>
<script> 
var text = '{ "name":"John", "birth":"1986-12-14", "city":"New York"
}';
var jsonScript = JSON.parse(text);
var titledata = jsonScript.name; 
alert(titledata);
</script>
</body>
</html> 

I am trying to write a php script that uses a Stock symbol (ex. AAPL for Apple and MSFT for Microsoft) to request a XML file containing stock news about the searched company. 我正在尝试编写一个使用Stock符号的php脚本(例如,Apple的AAPL和Microsoft的MSFT)来请求一个XML文件,其中包含有关被搜索公司的股票新闻。 All the stock news come from the Seeking Alpha Stock News RSS feed. 所有股票新闻均来自Seeking Alpha股票新闻RSS feed。 The response is a XML-Formatted object. 响应是XML格式的对象。 The php script should parse the returned XML-formatted object, extract the necessary fields and build a JSON object to be sent to the client. php脚本应解析返回的XML格式的对象,提取必要的字段并构建一个JSON对象以发送给客户端。 I am using the following php code for that: 我为此使用以下php代码:

$note = "https://seekingalpha.com/api/sa/combined/".$symbol.".xml";
$xml=simplexml_load_file($note) or die("Error: Cannot create object");
$jsonNews = json_encode($xml);

The returned json data seems to have some problem with the formatting. 返回的json数据似乎在格式化方面存在问题。 I have created a sample json data which is of the same format as the json returned by php script. 我创建了一个示例json数据,其格式与php脚本返回的json格式相同。 If the format is incorrect, how do i correct the format? 如果格式不正确,如何纠正格式?

It is not the format of the json: 它不是json的格式:

"aaa
bbb" 

is not a valid string (see the coloring of the code (Edit: I mean in the image)) 不是有效的字符串(请参见代码的颜色(编辑:我的意思是图像中的内容))

You could do 你可以做

"aaa"+
"bbb"

(same as "aaabbb") or (与“ aaabbb”相同)或

"aaa\nbbb" 

if you wish to include a newline in the string 如果您希望在字符串中包含换行符

Edit: So in your case: 编辑:因此在您的情况下:

'{"name": "John","birth": "1986-12-14","city": "new York"
}'  - not a valid json string because not a valid string



'{"name": "John","birth": "1986-12-14","city": "new York"'+
'}'  - a valid json string



'{"name": "John","birth": "1986-12-14","city": "new York"\n}'  - a valid json string with a newline

You don't need define JSON objects from strings, that is causing you an error typing incorrectly the last '}' symbol out of your string var. 您不需要从字符串定义JSON对象,这会导致您错误输入字符串var中最后一个'}'符号。 You should place write there: +'}'; 您应该在此处写:+'}';

Nevertheless JSON is the Javascript Object Notation, so you can use it as an object definition: 不过,JSON是Javascript Object Notation,因此您可以将其用作对象定义:

var jsonScript = {
    name: "Jhon",
    birth: "1986-12-14",
    city: "New York"
}

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

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