Languages

Menu
Sites
Language
JSON parse external source

Dear, 

Is there a simple way to parse a JSON with Tau library? I couldn't find any solution.

Json example: www --> alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo

 

Thanks

Responses

3 Replies
Slawek Kowalski

var myJSONString = ... // load from a file

var json = JSON.parse(myJSONString);

 

Ismael Sanchez

You can use regular XMLHttpRequest in Tizen

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    //do whatever you want to do with parsed JSON file
    console.log(myObj); // show parsed object in console
  }
};
xmlhttp.open("GET", "www -->alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo", true);
xmlhttp.send();

gui reif

Thank you! It worked both.

It worked, but another user in Stackoverflow recommended to setup config.xml with:

<tizen:privilege name="http://tizen.org/privilege/internet"/>

and

<access origin="https://www.alphavantage.co" subdomains="true"/>

 

Then I've come up with two working options:

 

Using fecth()

if (self.fetch) {
  console.log("Fetching...")
  fetch('https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo')
    .then(response => response.json())
    .then(data => {
      console.log(data['Global Quote']['01. symbol'])
    })

} else {
  console.log("Something went wrong...")
}

 

Using XMLHttpRequest()

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {

  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    console.log("Ok!");
    console.log(myObj['Global Quote']['01. symbol']);
  }
};

xmlhttp.open('GET', 'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo', true);
xmlhttp.send();

 

Thank you guys! Hope to get my wearable watch app on play store soon :)