0

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 codeenter image description here 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 Codeenter image description here 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?

user45437
  • 183
  • 3
  • 15
  • 6
    Please don't post images of code; it's not searchable and we can't copy/paste it into answers. – Paul Abbott Oct 16 '17 at 22:19
  • 2
    I don't see a JSON file. – Felix Kling Oct 16 '17 at 22:24
  • 2
    *"If the format is incorrect, how do i correct the format?"* The format of the JSON is fine. The problem is that a string literal cannot spawn multiple lines in JavaScript. – Felix Kling Oct 16 '17 at 22:26
  • *"The returned json data seems to have some problem with the formatting."* As I have mentioned before the format of the JSON is fine. But you cannot simply put it inside a JavaScript string literal be cause string literals cannot spawn multiple lines. There is almost never a reason to put JSON in a string literal. – Felix Kling Oct 16 '17 at 22:57

2 Answers2

0

It is not the format of the 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

"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
user2887596
  • 641
  • 5
  • 15
0

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. You should place write there: +'}';

Nevertheless JSON is the Javascript Object Notation, so you can use it as an object definition:

var jsonScript = {
    name: "Jhon",
    birth: "1986-12-14",
    city: "New York"
}
Camilo Ortegón
  • 3,414
  • 3
  • 26
  • 34
  • How do i correct the format of a json data returned by a php script ? – user45437 Oct 16 '17 at 22:40
  • @user45437: If you are generating the JavaScript code from PHP then you should *not* put the JSON in a string literal. Maybe this helps: [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/q/23740548/218196) (especially approach 3 in the accepted answer). – Felix Kling Oct 16 '17 at 22:55