I am trying to test a simple code where I am extracting data from JSON file and displaying an alert. I am confused about the format of the JSON file.
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.
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. All the stock news come from the Seeking Alpha Stock News RSS feed. The response is a XML-Formatted object. 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. I am using the following php code for that:
$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. I have created a sample json data which is of the same format as the json returned by php script. If the format is incorrect, how do i correct the format?