让 Greasemonkey 解释 JSON 数据
我正在尝试让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该 JSON 包含一个二维数组,因此请考虑以这种方式访问数据。
例如,给定的 JSON 返回 d 作为 11 x 17 数组。
行就像:
我假设您可以弄清楚列是什么(没有为我们提供足够的信息)。
所以,这是一个完整的 GM 脚本,其中列出了 Firebug 控制台的所有第 7 列......
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:
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...
您看到的是 JSON - JavaScript 对象表示法 - 它是一种轻量级数据交换格式,具有非常简洁的优势它是 JavaScript 的子集,因此 JSON 字符串(解析后)可用作 JavaScript 对象。
jQuery 与 JSON 字符串无关(除了它可以生成和解析它们)——它是一个 JavaScript 库,可以让你更轻松地完成很多事情,但没有什么是你在普通 JavaScript 中做不到的。
要使用 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 ()
是一个坏主意”。对于您发布的结构,如果您设置对象文字的格式,您将看到它是一个具有名为
d
的属性的对象,该属性是一个数组。该数组的内容就是数组本身。因此,一旦 JSON 字符串被解析为对象,您就可以执行以下操作:如果您使用 jQuery,则可以使用
$.each()
在外循环的每次迭代中,
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.
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 thegetJSON()
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 forajax()
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 "usingeval()
is a bad idea".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:If you're using jQuery, you could use
$.each()
In each iteration of the outer loop,
currEntry
will give you a reference to an array insided
. 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"
.