json request choose randomly

0 views
Skip to first unread message

hamburger

unread,
Jun 29, 2009, 8:55:14 AM6/29/09
to MooTools Users
Hello,
I would like to choose randomly some arrays out of my json.request (by
parameter)
How can i do this?

var number_to_choose = 3;

var request = new Request.JSON({
url: path + 'recent.json',
onComplete: function(jsonObj) {
//var
rand = $random(0, jsonObj.lending_actions.length-1);
//addText(jsonObj.lending_actions[rand]);
addText(jsonObj.lending_actions);
}
}).send();

var addText = function(addArchiv) {

zufall =$random(0,99);
var name = new Element('h2', {'html': 'Hallo'});

addArchiv.each(function(item, index) {
if(index==zufall)
{
var liste = new Element('ul', {'class': 'recentlist'}).inject
(name);
var el = new Element('li', {'html': 'index:'+index+' item:
'+item.id+'&nbsp;<a href="'+item.date+'"
class="loadMe">'+item.loan.use}).inject(liste);
}
});

name.inject(sidebar);

};

electronbender

unread,
Jul 2, 2009, 11:30:55 AM7/2/09
to MooTools Users
it would help if we knew what the json looks like

hamburger

unread,
Jul 3, 2009, 5:37:04 AM7/3/09
to MooTools Users
Here is the json:

{"lending_actions":[

{"id":16113424,"date":"2009-06-26T16:06:13Z",
"loan":{
"id":118782,
"name":"Emma Metuda",
"status":"fundraising",
"loan_amount":150,
"funded_amount":0,
"basket_amount":0,
"borrower_count":1,

"image":{"id":344096,"template_id":1},

"activity":"General Store",
"sector":"Retail",
"use":"To use as additional capital for her sari-sari store",
"location":{"country_code":"PH","country":"Philippines","town":"Pan-
ay ,clarin,misamis Occidental","geo":
{"level":"town","pairs":"0","type":"point"}},
"partner_id":126,
"posted_date":"2009-06-26T15:50:06Z",
"description":{"languages":["en","en"]}},
"lender":{"lender_id":"pedro2672","name":"Pedro","whereabouts":"S.
Domingos de Rana","country_code":"PT","uid":"pedro2672"}},


{"id":16113423,"date":"2009-06-26T16:06:08Z","loan":{"id":
117887,"name":"Olena Popova","status":"fundraising","loan_amount":
2000,"funded_amount":0,"basket_amount":0,"borrower_count":1,"image":
{"id":342327,"template_id":1},"activity":"Cosmetics
Sales","sector":"Retail","use":"to increase her inventory of
cosmetics","location":
{"country_code":"UA","country":"Ukraine","town":"Kherson","geo":
{"level":"town","pairs":"46.633333 32.6","type":"point"}},"partner_id":
26,"posted_date":"2009-06-23T20:30:04Z","description":{"languages":
["en","en"]}},"lender":{"lender_id":"iyad7331","name":"Iyad","image":
{"id":240974,"template_id":1},"whereabouts":"San Jose
CA","country_code":"US","uid":"iyad7331"}},
...
> >         };- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

electronbender

unread,
Jul 10, 2009, 11:03:31 AM7/10/09
to MooTools Users
Well, since lending actions is an array, you can start there:

var number_to_choose = 3;
var request = new Request.JSON({
url: path + 'recent.json',
onComplete: function(jsonObj) {
var notimes = 3;
// I could be wrong about this one, but you generally want a loop that
does this X times, right?
notimes.each(function(time){
var RandEntry = jsonObj.lending_actions.getRandom()
// do stuff with the entry... make sure you check you didnt get it
already, so you dont duplicate...
})
Reply all
Reply to author
Forward
0 new messages