14 Replies Latest reply on Jan 16, 2018 8:44 AM by ggraham123

    How do I read variable in JSON file?

    jiggy1965 Level 1

      As a test I've got this JSON file called info.js:

       

      var JSONObject = '{"page_text" : "This a normal string that can be used."}'
      

       

      Now in my Animate CC file I want to read that 'page_text' variable so I can put its content, the string, in a text field. I started with this:

       

      // load JSON file  
      var req = new XMLHttpRequest();  
      req.open("GET", "info.js", true);  
      req.addEventListener("load", transferComplete);  
      req.send();  
      
      
      //output JSON file
      function transferComplete(e) {  
          var JSONObject= JSON.parse(e.target.response);
        console.log(JSONObject);
      }  
      

       

      This should read the info.js file. Which works. Then it should output the contents of the JSON file in the console. Without that JSON.parse it outputs the entire contents as a string. So it kind of works, loading the JSON file and outputting the entire contents.

       

      But I wish the output the content of the page_text variable. So I was thinking the first thing I'd have to do is to JSON parse  it. But then it outputs: 'Uncaught SyntaxError: Unexpected token v in JSON at position 0'. Don't know what I'm doing wrong/

       

      How to I make it output "This is a normal string that can be used."?