Getting Greasemonkey to interpret JSON data

I'm trying to get Greasemonkey to automate the site for me, and I don't want to retrieve information one at a time. So I looked around and discovered jQuery. Being very new to Greasemonkey scripts, I still find this a bit tough, but if someone can point me in the right direction, it will help a lot. I want Greasemonkey to extract information in files that I believe are related to jQuery. When I launch 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 information 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 understand the information, and it is updated every second, but how do I get Greasemonkey to interpret the information? 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 retrieve this information?

Thank you very much.

+1
source share
2 answers

This JSON contains a 2-dimensional array, so think about accessing data this way. For example, a given JSON returns d as an 11 by 17 array.

The lines 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 understand what columns are (not enough information for us).

So here is the full GM script that lists all 7th columns in the Firebug 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]); } ); 
+3
source

What you are looking at is JSON - JavaScript Object Designator - this is a lightweight data exchange format with a very neat advantage that it is a subset of JavaScript, so the JSON string (after parsing) can be used 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 simplifies many things, but nothing you could do in plain JavaScript.

  • To work with JSON, you need a JSON parser to convert a JSON string representation into an object.

    a. If you are using jQuery, you can check the parseJSON() function. Alternatively, it might make more sense to use getJSON() when retrieving data - this way jQuery will automatically extract and parse the JSON response. As indicated on the documentation page, this is a really convenient method for ajax() with the appropriate parameters.

    b. On the other hand, if you are not using jQuery (and jQuery, which is included in the site page, is not taken into account), you can use json-2 for JavaScript.

    from. You can rely on your own browser JSON processing capabilities (not recommended)

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

  • For the structure you posted, if you format the object literal, you will see that it is an object with a property named d , which is an array. The contents of this array are the arrays themselves. So, once the JSON string has been parsed into an object, you can 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 are using jQuery you can 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 the array inside d . You can then iterate over this array to get the values ​​for each row. If you already know which row you want, you can also index it directly (beware of non-existent indexes). data.d[4][10] will provide you with a "ZAR" .

+1
source

All Articles