3

Is it possible to get JSON from a webpage for use in a windows desktop gadget and convert it to an array via javascript?

An example would be excellent.

user556396
  • 597
  • 3
  • 12
  • 26
  • Just to clarify, do you want to scrape a web page, or generate the json via a script and output purely json code? – PottyBert Jan 06 '11 at 20:53
  • The JSON is part of an api so it's already there so yes scraping. – user556396 Jan 06 '11 at 21:04
  • Check out this blog post on [Building a Windows Sidebar Gadget](http://blog.wassupy.com/2009/08/building-windows-sidebar-gadget.html) for an example. – Matt V. Jan 06 '11 at 22:13

1 Answers1

0

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];
Rafael Barros
  • 2,738
  • 1
  • 21
  • 28
genichm
  • 525
  • 7
  • 18