简体   繁体   English

使用TAU库在Tizen Web App中解析JSON

[英]Parse JSON in Tizen Web App with TAU Library

Is there a simple way to parse a JSON with TAU library? 有没有一种简单的方法可以使用TAU库解析JSON? I couldn't find any solution. 我找不到任何解决方案。

I'm trying to get data from alphavantage api and display it: www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo 我正在尝试从alphavantage api获取数据并显示它: www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo

I've tried XMLhttprequest and Jquery and none seems to work with Tizen Web App. 我已经尝试过XMLhttprequest和Jquery,但似乎都无法与Tizen Web App一起使用。

To access external resources You need to add internet privilege and define access origin (both in config.xml ): 要访问外部资源,您需要添加Internet 特权并定义访问源 (都在config.xml ):

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

and

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

then You can simply get data with Fetch API: 那么您只需使用Fetch API获取数据:

fetch('https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo')
    .then(res => res.json())
    .then(jsonData => { console.log(jsonData) });

After setting up the config.xml as recommended above by @Patryk Falba, 在按照@Patryk Falba的建议设置config.xml之后,

I've come up with two working options: 我提出了两个工作选择:

Using fecth() 使用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() 使用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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM