让Greasemonkey解释JSON数据 [英] Getting Greasemonkey to interpret JSON data
问题描述
我正在尝试让Greasemonkey为我自动化网站,我不想一次提取一个信息。所以我浏览了一下并发现了jQuery。作为Greasemonkey脚本的新手,我仍然觉得它有点难,但如果有人能指出我正确的方向,那将会有很大的帮助。我希望Greasemonkey提取我认为链接到jQuery的文件中的信息。当我在页面上运行Firebug时,我得到:
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
里面有这个信息:
{
"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行的每条信息都被读入自己的变量:
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
如何让Greasemonkey提取该信息?
How do I get Greasemonkey to extract that info?
非常感谢。
推荐答案
JSON包含一个二维数组,所以想想以这种方式访问数据。
例如,给定的JSON返回 d
作为11乘17的数组。
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.
行是喜欢:
["", "", "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).
所以,这是一个完整的GM脚本,它列出了Firebug控制台的所有第7列......
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]);
}
);
这篇关于让Greasemonkey解释JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!