开发者

Windows Gadget: How do i get json from a webpage and convert to a series of arrays with javascript?

Is it possible to get JSON from a webpage for u开发者_运维知识库se in a windows desktop gadget and convert it to an array via javascript?

An example would be excellent.


It's late to answer, but it may be useful to somebody else. I'm developing windows gadget and can not use JSON.parse(string) of eval(string) to convert string returned from server into json it simply do not work, but i have found some strange way to do it.

var json = (eval("[" + eval(json string) + "]"))[0]; //magic but works (btw creates json array as required in the question, all that required is to remove [0] in the end).

Complete code example:

function syncRequest(_url, _data) {
    var req = new XMLHttpRequest();
    req.open("POST", _url, false);
    req.setRequestHeader("Content-type", "application/json");
    req.send(_data);
    return req.responseText;
}

var response = syncRequest("http://...", "{json data}");

//here response converted into json
var json = (eval("[" + eval(response) + "]"))[0];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜