让 Greasemonkey 解释 JSON 数据

发布于 2024-11-09 06:40:23 字数 2276 浏览 0 评论 0原文

我正在尝试让 Greasemonkey 为我自动化一个网站,但我不想一次提取一个信息。所以我环顾四周并发现了 jQuery。作为 Greasemonkey 脚本的新手,我仍然觉得有点困难,但如果有人能给我指出正确的方向,那将会有很大帮助。我希望 Greasemonkey 提取我认为链接到 jQuery 的文件中的信息。当我在页面上运行 Firebug 时,我得到:

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

包含以下信息:

{
    "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 ]
    ]
}

我可以理解该信息,并且每秒更新一次,但如何让 Greasemonkey 解释该信息?举例来说,我希望将第 4 行上的每条信息读入自己的变量中:

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

如何让 Greasemonkey 提取该信息?

多谢。

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 each 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-11-16 06:40:23

该 JSON 包含一个二维数组,因此请考虑以这种方式访问​​数据。
例如,给定的 JSON 返回 d 作为 11 x 17 数组。

行就像:

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

我假设您可以弄清楚列是什么(没有为我们提供足够的信息)。

所以,这是一个完整的 GM 脚本,其中列出了 Firebug 控制台的所有第 7 列......

// ==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]);
    }
);

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]);
    }
);
煮茶煮酒煮时光 2024-11-16 06:40:23

您看到的是 JSON - JavaScript 对象表示法 - 它是一种轻量级数据交换格式,具有非常简洁的优势它是 JavaScript 的子集,因此 JSON 字符串(解析后)可用作 JavaScript 对象。

jQuery 与 JSON 字符串无关(除了它可以生成和解析它们)——它是一个 JavaScript 库,可以让你更轻松地完成很多事情,但没有什么是你在普通 JavaScript 中做不到的。

  1. 要使用 JSON,您需要一个 JSON 解析器来将 JSON 字符串表示形式转换为对象。

    a.如果您使用 jQuery,则可以查看 parseJSON()< /a> 函数。或者,使用 getJSON() 获取数据时 - 这样 jQuery 将自动获取并解析 JSON 响应。正如文档页面所述,这确实是 ajax() 使用适当的参数。

    b.另一方面,如果您不使用 jQuery(并且网站页面中包含的 jQuery 不算在内),您可以使用 json-2 JavaScript 库。

    c.您可以依赖浏览器的本机 JSON 处理功能(不推荐)

    d.正如 RobG 在下面的评论中指出的那样,总是有 eval ()。您可能想阅读此答案,然后< a href="https://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil">这个答案来了解为什么和为什么不使用eval ()是一个坏主意”。

  2. 对于您发布的结构,如果您设置对象文字的格式,您将看到它是一个具有名为 d 的属性的对象,该属性是一个数组。该数组的内容就是数组本身。因此,一旦 JSON 字符串被解析为对象,您就可以执行以下操作:

    var data = parseJSON(jsonString); //获取一个JavaScript对象
    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];
            控制台.log(currVal);
        }
    }
    

    如果您使用 jQuery,则可以使用 $.each()

    var data = parseJSON(jsonString);
    $.each(data.d, 函数(索引, currEntry){
        $.each(currEntry, 函数(索引, currVal){
            控制台.log(currVal);
        }
    });
    

    在外循环的每次迭代中,currEntry 将为您提供对 d 内部数组的引用。然后,您可以迭代该数组以获取每行的值。如果您已经知道所需的“行”,您也可以直接对其进行索引(但要注意不存在的索引)。 data.d[4][10] 将为您提供 "ZAR"

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文