2
votes

I'd like to save the result of my YQL Query in an javascript object

my query:

SELECT * FROM html WHERE url="http://myurl.com" and xpath="/html/body/center/table[1]/tr"

how do I have to go on? I read the doc of YQL but I think it's really complicated. I also searched on stackoverflow but it doesn't really help me.

the object should just be like a normal JSON object in JS.

regards

2
Are you really just looking for a way to parse the JSON string response to an object? - Bergi

2 Answers

1
votes

You can get this data using JSONP approach. Script:

<script src="http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20html%20WHERE%20url%3D%22http%3A%2F%2Fghse%3A12-13%40www.ghse.de%2Fvplan%2F12%2Fc%2Fc00082.htm%22%20and%20xpath%3D%22%2Fhtml%2Fbody%2Fcenter%2Ftable%5B1%5D%2Ftr%22&format=json&callback=callback"></script>

Callback to be used to process response:

function callback(data) {
    console.log(data);
}

http://jsfiddle.net/bdCHz/

Also take a look at YQL console tester for detailed information.

This is just an example how you can retrive this JSON manually. You can use jQuery, etc. to issue JSONP request.

1
votes

You may not be able to save the whole thing in one lump, but you could save the data by grabbing the information you need specifically, organize it into an array, and save that array:

function getXML(Your_XML_URL) {
// Build the YQL query
var qryRSS = 'select * from rss where url=' + '"' + feed + '"';

// Initiate the YQL query
$.getJSON("http://query.yahooapis.com/v1/public/yql",
    {
        // These are the settings for your API call
        q: qryRSS,
        format: "json"
    },
    // Take the data you got from the YQL server and output it to the screen. The variable "data" holds the JSON you got back from the YQL server.
    function (data) {
        var myArrayName = [];
        // Create an object for each entry. If you don't want every entry in the XML files you can change data.query.results.item.length to whatever number you want.
        for (var i = 0; i < data.query.results.item.length; i += 1) {
            var singleEntry = {};
            dataLoc = data.query.results.item[i];

            var singleEntry.title = dataLoc.title;           //You would replace these
            var singleEntry.pub = dataLoc.pubDate;           //with the names of the tags
            var singleEntry.image = dataLoc.thumbnail.url;   //in your XML file.
            var singleEntry.url = dataLoc.link;
            var singleEntry.desc = dataLoc.description;

            //Add your object to the array.
            myArrayName.push(singleEntry);

        }
        localStorage.setItem("mySavedItem", JSON.stringify(myArrayName));
    });
}

Then you can retrieve that information later with:

var myXMLArray = JSON.parse(localStorage.getItem("mySavedItem"));
console.log(myXMLArray[0].title);
console.log(myXMLArray[2].pub);