开发者

Getting Greasemonkey to interpret JSON data

开发者 https://www.devze.com 2023-03-07 06:58 出处:网络
I\'m trying to get Greasemonkey to automate a site for me, and I don\'t want to extract the info one at a time. So I had a look around and discovered jQuery. Being very new to Greasemonkey scripts, I

I'm trying to get Greasemonkey to automate a site for me, and I don't want to extract the info one at a time. So I had a look around and discovered jQuery. Being very new to Greasemonkey scripts, I still find it a bit tough, but if someone could point me in the right direction it would help a lot. I want Greasemonkey to extract information in files that I presume are linked to jQuery. When I run Firebug on the page I get:

http://www.trada.net/javascript/jquery-1.4.2.min.js
http://www.trada.net/REST_Service/REST_Auction.svc/GetAuctionData?_=1306009003654

with this info inside:

{
    "d": [
        [ "", "", "y", "ZAR", "1", "49517", "8270, 8270, 8270, 7635, 8270", null,
          "1.34", "8270", "0:13:30", "", "12", "", "C", "30", null ],
        [ "", "", "y", "ZAR", "2", "49582", "6725, 6725, 7863, 9228", null,
          "***0.78***", "6725", "0:12:37", "", "5", "", "L", null, null ],
        [ "", "", "y", "ZAR", "3", "49058", "5153, 9216, 6058, 9216, 5153", null,
          "180.80", "5153", "0:00:59", "", "1100", "", "T", null, null ],
        [ "", "", "y", "ZAR", "4", "49581", "0051, 6692, 9555, 6692, 9555", null,
          "1.35", "0051", "0:00:14", "", "12", "", "T", null, null ],
        [ "", "", "y", "ZAR", "5", "49584", "6725, 6725, 9822", null,
          "0.93", "6725", "0:14:28", "", "5", "", "L", null, null ],
        [ "", "", "y", "ZAR", "6", "49583", "9822, 7863, 9228", null,
          "0.75", "9822", "0:15:05", "", "5", "", "L", null, null ],
        [ "", "", "y", "ZAR", "7", "49544", "0957, 0957, 0957, 0957, 0957", null,
          "10.00", "0957", "0:01:59", "", "55", "", "T", null, null ],
        [ "", "", "y", "ZAR", "8", "49575", "2110, 5661, 9295, 2110, 3809", null,
          "3.05", "2110", "0:00:13", "", "29", "", "T", null, null ],
        [ "", "", "y", "ZAR", "9", "49496", "7863, 5845, 7863, 7158, 7158", null,
          "2.41", "7863", "0:05:55", "", "10", "", "B", null, null ],
        [ "", "", "y", "ZAR", "10", "49524", "7863, 7863, 5845, 7863, 0764", null,
          "1.57", "7863", "0:05:49", "", "5", "", "B", null, null ],
        [ "", "", "y", "ZAR", "11", "49539", "7863, 7863, 0764, 2427, 2427", null,
          "1.92", "7863", "0:03:54", "", "10", "", "B", null, null ]
    ]
}

I can make sense of the info, and it is renewed every second, but how do I get Greasemonkey to interpret the info? Say for example I want e开发者_Python百科ach piece of information on line 4 to be read into its own variable:

"1.34", "8270", "0:13:30", "", "12", "", "C", "30", null

How do I get Greasemonkey to extract that info?

Thanks a lot.


That JSON contains a 2-D array, so think of accessing the data that way. For example, the given JSON returns d as an 11 by 17 array.

The rows are like:

["", "", "y", "ZAR", "1", "49517", "6458, 8270, 8270, 8270, 7635", null, "1.40", "6458", "0:13:30", "", "12", "", "C", "30", null]

I assume you can figure out what the columns are (not enough info provided for us to).

So, here's a complete GM script that lists all of the 7th columns to Firebug's console...

// ==UserScript==
// @name            _Fun with JSON
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==

var myJson              = '{"d":[["","","y","ZAR","1","49517","6458, 8270, 8270, 8270, 7635",null,"1.40","6458","0:13:30","","12","","C","30",null],["y","-00:00","y","ZAR","2","49593","6458, 6458, 6458, 6458, 6458",null,"2.92","6458","0:13:37","","12","","L","12","Ve4mYdrvkkQMKxBH1\/1VMtDTCDQBRspg5jB8jjY08zg="],["","","y","ZAR","3","49058","7456, 9216, 6458, 5153, 7456",null,"194.40","7456","0:00:31","","1100","","T",null,null],["","","y","ZAR","4","49597","2935, 6554",null,"1.22","2935","0:01:16","","12","","T",null,null],["","","y","ZAR","5","49590","4440, 0518, 5343, 2625, 4848",null,"0.95","4440","0:15:58","","5","","L",null,null],["","","y","ZAR","6","49591","4848, 4440, 4440, 0518, 2625",null,"1.81","4848","0:16:05","","12","","L",null,null],["","","y","ZAR","7","49595","6458",null,"5.55","6458","0:04:13","","55","","T",null,null],["","","y","ZAR","8","49596","",null,"2.90","NONE","0:04:35","","29","","T",null,null],["","","y","ZAR","9","49496","6458, 2427, 2427, 7863, 5845",null,"2.56","6458","0:06:07","","10","","B",null,null],["","","y","ZAR","10","49524","6458, 2427, 7863, 7863, 5845",null,"1.67","6458","0:06:00","","5","","B",null,null],["","","y","ZAR","11","49539","6458, 2427, 7863, 7863, 0764",null,"2.02","6458","0:04:25","","10","","B",null,null]]}'
var jsonObj             = $.parseJSON (myJson);

//--- The JSON should return a 2-D array, named "d".
var arrayOfAuctions     = jsonObj.d;

//--- Loop over each row in the array.
$.each (
    arrayOfAuctions,
    function (rowIndex, singleAuctionData) {

        //--- Print the 7th column.
        console.log ('Row: ' + (parseInt (rowIndex) + 1) + ' Column: 7  Value: ' + singleAuctionData[6]);
    }
);


What you're looking at is JSON - JavaScript Object Notation - it's a lightweight data-interchange format with the very neat advantage that it's a subset of JavaScript so a JSON string (once parsed) is usable as a JavaScript object.

jQuery has nothing to do with JSON strings (except that it can generate and parse them) - it is a JavaScript library that makes it easier to do a lot of things but nothing that you couldn't do in plain JavaScript.

  1. To work with JSON, you'll need a JSON parser to convert the JSON string representation into an object.

    a. If you're using jQuery, you can check out the parseJSON() function. Alternatively, it might make more sense to use the getJSON() when getting the data - this way jQuery will automatically fetch and parse the JSON response. As noted on the documentation page, this is really a convenience method for ajax() with the appropriate parameters.

    b. On the other hand, if you're not using jQuery (and jQuery being included in the website's page doesn't count), you could make use of the json-2 library for JavaScript.

    c. You could rely on a browser's native JSON handling capabilities (not recommended)

    d. As RobG notes in the comment below, there's always eval(). You might want to read this answer and then this answer to see why and why not "using eval() is a bad idea".

  2. For the structure you've posted, if you format the object literal, you'll see that it's an object with a property named d which is an array. The contents of that array are arrays themselves. So, once the JSON string has been parsed into an object,you could do this:

    var data = parseJSON(jsonString); //get a JavaScript objet
    for(var i = 0; i < data.d.length; i++){
        var currEntry = data.d[i];
        for(var j = 0; j < currEntry.length; j++){
            var currVal = currEntry[j];
            console.log(currVal);
        }
    }
    

    If you're using jQuery, you could use $.each()

    var data = parseJSON(jsonString);
    $.each(data.d, function(index, currEntry){
        $.each(currEntry, function(index, currVal){
            console.log(currVal);
        }
    });
    

    In each iteration of the outer loop, currEntry will give you a reference to an array inside d. You can then iterate over that array to get the values for each line. If you already know which 'line' you want, you could also index into it directly (beware of non-existent indices though). data.d[4][10] will give you "ZAR".

0

精彩评论

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

关注公众号