Read Json with ajax with names without quotes

0

I am looking to read through jquery and ajax an object json from an external server. When making the request, the server returns a supposed json file with the following format:

{
 Status: 'Success',
 Data:{
 'primero': {
    phone:'5281128043'
   }
 }
} 

Where the names of the values are without quotation marks.

The request is made using the following HTML tag:

<script type="text/Javascript" src="http://www.servlets.org/Ads.aspx?
&callback=myCallback"></script>

Since the information comes from an external server, I have no control over its syntax and upon request it returns the following error:

  

SyntaxError: missing; before statement.

How could I receive this json file written in that format without quotes? Maybe with some attribute type different inside the script tag?

    
asked by Julian Garzon 04.04.2017 в 20:43
source

2 answers

3

The format is in ... Nononono, it's totally correct .

JSON is actually a piece of Javascript that is incorporated into the code; this piece of code represents a data structure (a simple element, an array of elements, an associative table of elements)

In

{"nombre" : "pedro"} 

you have an associative array with a single element, with key nombre and value pedro .

JS allows you to remove the quotes from the element on the left and still consider it as a label; although not so with the one on the right 1 . So:

{nombre : "pedro"}

It is exactly the same as the previous example.

The problem is that JSON is just the data . What you get from JSON must be assigned to a Javascript variable and then processed with your Javascript. What you are doing is loading an incomplete part of the program into the Javascript interpreter, which very logically complains.

What you have to do is, from the JavaScript code , make an Ajax call to the URL and assign the returned value to a Javascript variable. And from there, to get the data of the structure.

To see the difference, a complete JS statement (which does nothing, only defines the structure) would be:

var misDatos = {
    Status: 'Success',
    Data:{
      'primero': {
         phone:'5281128043'
         }
      }
    };

Ajax's grace is that the JS program and the data go separately, so you can reload the data without loading the whole page, get data from other servers, etc. The problem is that the way to obtain them is something more complicated than simply writing the JS code.

Here you have a good example to start (in English sorry, but if you search for "JSON Ajax" you will surely find more). link

1 If you remove the quotes from the element on the right, it interprets it as the name of a variable.     
answered by 04.04.2017 в 21:08
0

One thing is the JavaScript literal object notation, where you can write the keys without quotes, and another JSON, where the standard says you have to use double quotes. It does not have to be that way, but it's like that.

If you can not change anything on the server, you will need to change the returned string before pairing it.

Use this regular expression (I have converted it from PHP, I do not know 100% if it will work in JavaScript, although it should):

json = json.replace(/([{,])(\s*)([A-Za-z0-9_\-]+?)\s*:/g, '$1"$3":');

And then you already do:

var data = JSON.parse(json);

    
answered by 06.04.2017 в 01:59