JSON.parse() function doesn't take a valid JSON format


(Flavio) #1

Hi,

When attempting to read a JSON formatted data in Hype, JSON Parse function raises an error as: Unexpected identifier “object”, even when the provided data is valid in other javascript frameworks, like Appcelerator.

in HTML header, I am including the corresponding line to load the data like .js file as follow:

<script type="text/javascript" src="${resourcesFolderName}/data.js"></script>

then I have a setup function to read and decode the data.js by doing:

//Get data from JSON file
     window.myData = JSON.parse(data);

with this format (data.js) it produces the above error:

   var data = [
    {"id":"1", 
    "question":"something",
    "answer":"D",
    "A":"Xmod_write",
    "B":"All",
    "C":"None",
    "D":"both a) and b)",
    "E":"a), b) and c)",
    "status":""}
  ];

If I change the file (data.js) as follow by adding ’ symbol:

var data = '[
        {"id":"1", 
        "question":"something",
        "answer":"D",
        "A":"Xmod_write",
        "B":"All",
        "C":"None",
        "D":"both a) and b)",
        "E":"a), b) and c)",
        "status":""}
      ]';

it produces the following error at parse:

SyntaxError: Unexpected EOF
Error in setup: ReferenceError: Can't find variable: data

and if I try pure JSON format as follow:

"data" : [
            {"id":"1", 
            "question":"something",
            "answer":"D",
            "A":"Xmod_write",
            "B":"All",
            "C":"None",
            "D":"both a) and b)",
            "E":"a), b) and c)",
            "status":""}
          ]

and this is the error produced:

SyntaxError: Unexpected token ':'. Parse error.
(anonymous function) data.js:1 HYPE-466.thin.min.js:14:99
Error in setup: ReferenceError: Can't find variable: data

However the only format that worked for me so far was:

data = '[{"id": "1", "question":"something", "answer": "D", "A": "Xmod_write", "B": "All", "C": "None", "D": "both a) and b)", "E": "a), b) and c)", "status": "noAnswered"},{"id": "2", "question":"something", "answer": "D", "A": "Xmod_write", "B": "All", "C": "None", "D": "both a) and b)", "E": "a), b) and c)", "status": "noAnswered"}]';

So, question is why only this format is accepted in Hype when the other format still valid for pure javascript code?

Regards,
Flavio


(Hans-Gerd Claßen) #2

why are you trying to parse a valid js as Json?
you’re first attempt sets var data to a array with one element that is a object. so data[0] in any hypefunction will return the object …


(Flavio) #3

Hi,

Because I need a local file to be load. It won’t be in the server. This content will be part of a mobile application. Therefore as far as I understand the JSON file has to be on the server.

In order to avoid that, a .js file can be loaded locally.

If you have other method to achieve this, I am all ears.

Regards,


(Flavio) #4

Yes, you right.

It’s because I started with a JSON file that was supposed to be remote, then local and then I mixed things up.

Indeed there is no need to decode if array declaration comes in .js file.

Thanks,
Flavio