2017-07-28 74 views
-1

現在,我有引號的JSON文件,每次代碼運行時我都會獲得所有值。現在,我怎樣才能得到只有一(1)個值,並且每當我的頁面加載或刷新時,它都會改變或隨機化值。如何限制JSON值並隨機化頁面加載中的每個數據

儘管我在how to get random json data and append to div element上獲得了隨機化JSON文件的代碼,但它很難理解。

如何獲取一(1)個隨機JSON值?

的jsfiddle:https://jsfiddle.net/aice09/Lp9xp69z/

var data=[{ 
 
    "quoteText": "Genius is one percent inspiration and ninety-nine percent perspiration.", 
 
    "quoteAuthor": "Thomas Edison" 
 
    }, { 
 
    "quoteText": "You can observe a lot just by watching.", 
 
    "quoteAuthor": "Yogi Berra" 
 
    }, { 
 
    "quoteText": "A house divided against itself cannot stand.", 
 
    "quoteAuthor": "Abraham Lincoln" 
 
    }, { 
 
    "quoteText": "Difficulties increase the nearer we get to the goal.", 
 
    "quoteAuthor": "Johann Wolfgang von Goethe" 
 
    }, { 
 
    "quoteText": "Fate is in your hands and no one elses", 
 
    "quoteAuthor": "Byron Pulsifer" 
 
    }, { 
 
    "quoteText": "Be the chief but never the lord.", 
 
    "quoteAuthor": "Lao Tzu" 
 
    }, { 
 
    "quoteText": "Nothing happens unless first we dream.", 
 
    "quoteAuthor": "Carl Sandburg" 
 
    }, { 
 
    "quoteText": "Well begun is half done.", 
 
    "quoteAuthor": "Aristotle" 
 
    }, { 
 
    "quoteText": "Life is a learning experience, only if you learn.", 
 
    "quoteAuthor": "Yogi Berra" 
 
    }, { 
 
    "quoteText": "Self-complacency is fatal to progress.", 
 
    "quoteAuthor": "Margaret Sangster" 
 
    }, { 
 
    "quoteText": "Peace comes from within. Do not seek it without.", 
 
    "quoteAuthor": "Buddha" 
 
    }, { 
 
    "quoteText": "What you give is what you get.", 
 
    "quoteAuthor": "Byron Pulsifer" 
 
    }, { 
 
    "quoteText": "We can only learn to love by loving.", 
 
    "quoteAuthor": "Iris Murdoch" 
 
    }, { 
 
    "quoteText": "Life is change. Growth is optional. Choose wisely.", 
 
    "quoteAuthor": "Karen Clark" 
 
    }, { 
 
    "quoteText": "You'll see it when you believe it.", 
 
    "quoteAuthor": "Wayne Dyer" 
 
    }, { 
 
    "quoteText": "Today is the tomorrow we worried about yesterday.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "It's easier to see the mistakes on someone else's paper.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "Every man dies. Not every man really lives.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "To lead people walk behind them.", 
 
    "quoteAuthor": "Lao Tzu" 
 
    }, { 
 
    "quoteText": "Having nothing, nothing can he lose.", 
 
    "quoteAuthor": "William Shakespeare" 
 
    }, { 
 
    "quoteText": "Trouble is only opportunity in work clothes.", 
 
    "quoteAuthor": "Henry J. Kaiser" 
 
    }, { 
 
    "quoteText": "A rolling stone gathers no moss.", 
 
    "quoteAuthor": "Publilius Syrus" 
 
    }, { 
 
    "quoteText": "Ideas are the beginning points of all fortunes.", 
 
    "quoteAuthor": "Napoleon Hill" 
 
    }, { 
 
    "quoteText": "Everything in life is luck.", 
 
    "quoteAuthor": "Donald Trump" 
 
    }, { 
 
    "quoteText": "Doing nothing is better than being busy doing nothing.", 
 
    "quoteAuthor": "Lao Tzu" 
 
    }, { 
 
    "quoteText": "Trust yourself. You know more than you think you do.", 
 
    "quoteAuthor": "Benjamin Spock" 
 
    }, { 
 
    "quoteText": "Study the past, if you would divine the future.", 
 
    "quoteAuthor": "Confucius" 
 
    }, { 
 
    "quoteText": "The day is already blessed, find peace within it.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "From error to error one discovers the entire truth.", 
 
    "quoteAuthor": "Sigmund Freud" 
 
    }, { 
 
    "quoteText": "Well done is better than well said.", 
 
    "quoteAuthor": "Benjamin Franklin" 
 
    }, { 
 
    "quoteText": "Bite off more than you can chew, then chew it.", 
 
    "quoteAuthor": "Ella Williams" 
 
    }, { 
 
    "quoteText": "Work out your own salvation. Do not depend on others.", 
 
    "quoteAuthor": "Buddha" 
 
    }, { 
 
    "quoteText": "One today is worth two tomorrows.", 
 
    "quoteAuthor": "Benjamin Franklin" 
 
    }, { 
 
    "quoteText": "Once you choose hope, anythings possible.", 
 
    "quoteAuthor": "Christopher Reeve" 
 
    }, { 
 
    "quoteText": "God always takes the simplest way.", 
 
    "quoteAuthor": "Albert Einstein" 
 
    }, { 
 
    "quoteText": "One fails forward toward success.", 
 
    "quoteAuthor": "Charles Kettering" 
 
    }, { 
 
    "quoteText": "From small beginnings come great things.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "Learning is a treasure that will follow its owner everywhere", 
 
    "quoteAuthor": "Chinese proverb" 
 
    }, { 
 
    "quoteText": "Be as you wish to seem.", 
 
    "quoteAuthor": "Socrates" 
 
    }, { 
 
    "quoteText": "The world is always in movement.", 
 
    "quoteAuthor": "V. Naipaul" 
 
    }, { 
 
    "quoteText": "Never mistake activity for achievement.", 
 
    "quoteAuthor": "John Wooden" 
 
    }, { 
 
    "quoteText": "What worries you masters you.", 
 
    "quoteAuthor": "Haddon Robinson" 
 
    }, { 
 
    "quoteText": "One faces the future with ones past.", 
 
    "quoteAuthor": "Pearl Buck" 
 
    }, { 
 
    "quoteText": "Goals are the fuel in the furnace of achievement.", 
 
    "quoteAuthor": "Brian Tracy" 
 
    }, { 
 
    "quoteText": "Who sows virtue reaps honour.", 
 
    "quoteAuthor": "Leonardo da Vinci" 
 
    }, { 
 
    "quoteText": "Be kind whenever possible. It is always possible.", 
 
    "quoteAuthor": "Dalai Lama" 
 
    }, { 
 
    "quoteText": "Talk doesn't cook rice.", 
 
    "quoteAuthor": "Chinese proverb" 
 
    }, { 
 
    "quoteText": "He is able who thinks he is able.", 
 
    "quoteAuthor": "Buddha" 
 
    }, { 
 
    "quoteText": "Be as you wish to seem.", 
 
    "quoteAuthor": "Socrates" 
 
    }, { 
 
    "quoteText": "A goal without a plan is just a wish.", 
 
    "quoteAuthor": "Larry Elder" 
 
    }, { 
 
    "quoteText": "To succeed, we must first believe that we can.", 
 
    "quoteAuthor": "Michael Korda" 
 
    }, { 
 
    "quoteText": "Learn from yesterday, live for today, hope for tomorrow.", 
 
    "quoteAuthor": "Albert Einstein" 
 
    }, { 
 
    "quoteText": "A weed is no more than a flower in disguise.", 
 
    "quoteAuthor": "James Lowell" 
 
    }, { 
 
    "quoteText": "Do, or do not. There is no try.", 
 
    "quoteAuthor": "Yoda" 
 
    }, { 
 
    "quoteText": "All serious daring starts from within.", 
 
    "quoteAuthor": "Harriet Beecher Stowe" 
 
    }, { 
 
    "quoteText": "The best teacher is experience learned from failures.", 
 
    "quoteAuthor": "Byron Pulsifer" 
 
    }, { 
 
    "quoteText": "Think how hard physics would be if particles could think.", 
 
    "quoteAuthor": "Murray Gell-Mann" 
 
    }, { 
 
    "quoteText": "Love is the flower you've got to let grow.", 
 
    "quoteAuthor": "John Lennon" 
 
    }, { 
 
    "quoteText": "Don't wait. The time will never be just right.", 
 
    "quoteAuthor": "Napoleon Hill" 
 
    }, { 
 
    "quoteText": "One fails forward toward success.", 
 
    "quoteAuthor": "Charles Kettering" 
 
    }, { 
 
    "quoteText": "Time is the wisest counsellor of all.", 
 
    "quoteAuthor": "Pericles" 
 
    }, { 
 
    "quoteText": "You give before you get.", 
 
    "quoteAuthor": "Napoleon Hill" 
 
    }, { 
 
    "quoteText": "Wisdom begins in wonder.", 
 
    "quoteAuthor": "Socrates" 
 
    }, { 
 
    "quoteText": "Without courage, wisdom bears no fruit.", 
 
    "quoteAuthor": "Baltasar Gracian" 
 
    }, { 
 
    "quoteText": "Change in all things is sweet.", 
 
    "quoteAuthor": "Aristotle" 
 
    }, { 
 
    "quoteText": "What you fear is that which requires action to overcome.", 
 
    "quoteAuthor": "Byron Pulsifer" 
 
    }, { 
 
    "quoteText": "The best teacher is experience learned from failures.", 
 
    "quoteAuthor": "Byron Pulsifer" 
 
    }, { 
 
    "quoteText": "When performance exceeds ambition, the overlap is called success.", 
 
    "quoteAuthor": "Cullen Hightower" 
 
    }, { 
 
    "quoteText": "When deeds speak, words are nothing.", 
 
    "quoteAuthor": "African proverb" 
 
    }, { 
 
    "quoteText": "Real magic in relationships means an absence of judgement of others.", 
 
    "quoteAuthor": "Wayne Dyer" 
 
    }, { 
 
    "quoteText": "When performance exceeds ambition, the overlap is called success.", 
 
    "quoteAuthor": "Cullen Hightower" 
 
    }, { 
 
    "quoteText": "I never think of the future. It comes soon enough.", 
 
    "quoteAuthor": "Albert Einstein" 
 
    }, { 
 
    "quoteText": "Skill to do comes of doing.", 
 
    "quoteAuthor": "Ralph Emerson" 
 
    }, { 
 
    "quoteText": "Wisdom is the supreme part of happiness.", 
 
    "quoteAuthor": "Sophocles" 
 
    }, { 
 
    "quoteText": "I believe that every person is born with talent.", 
 
    "quoteAuthor": "Maya Angelou" 
 
    }, { 
 
    "quoteText": "Important principles may, and must, be inflexible.", 
 
    "quoteAuthor": "Abraham Lincoln" 
 
    }, { 
 
    "quoteText": "The undertaking of a new action brings new strength.", 
 
    "quoteAuthor": "Richard Evans" 
 
    }, { 
 
    "quoteText": "I believe that every person is born with talent.", 
 
    "quoteAuthor": "Maya Angelou" 
 
    }, { 
 
    "quoteText": "The years teach much which the days never know.", 
 
    "quoteAuthor": "Ralph Emerson" 
 
    }, { 
 
    "quoteText": "Our distrust is very expensive.", 
 
    "quoteAuthor": "Ralph Emerson" 
 
    }, { 
 
    "quoteText": "All know the way; few actually walk it.", 
 
    "quoteAuthor": "Bodhidharma" 
 
    }, { 
 
    "quoteText": "Great talent finds happiness in execution.", 
 
    "quoteAuthor": "Johann Wolfgang von Goethe" 
 
    }, { 
 
    "quoteText": "Faith in oneself is the best and safest course.", 
 
    "quoteAuthor": "Michelangelo" 
 
    }, { 
 
    "quoteText": "Courage is going from failure to failure without losing enthusiasm.", 
 
    "quoteAuthor": "Winston Churchill" 
 
    }, { 
 
    "quoteText": "The two most powerful warriors are patience and time.", 
 
    "quoteAuthor": "Leo Tolstoy" 
 
    }, { 
 
    "quoteText": "Anticipate the difficult by managing the easy.", 
 
    "quoteAuthor": "Lao Tzu" 
 
    }, { 
 
    "quoteText": "Those who are free of resentful thoughts surely find peace.", 
 
    "quoteAuthor": "Buddha" 
 
    }, { 
 
    "quoteText": "Talk doesn't cook rice.", 
 
    "quoteAuthor": "Chinese proverb" 
 
    }, { 
 
    "quoteText": "A short saying often contains much wisdom.", 
 
    "quoteAuthor": "Sophocles" 
 
    }, { 
 
    "quoteText": "The day is already blessed, find peace within it.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "It takes both sunshine and rain to make a rainbow.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "A beautiful thing is never perfect.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "Only do what your heart tells you.", 
 
    "quoteAuthor": "Princess Diana" 
 
    }, { 
 
    "quoteText": "Life is movement-we breathe, we eat, we walk, we move!", 
 
    "quoteAuthor": "John Pierrakos" 
 
    }, { 
 
    "quoteText": "No one can make you feel inferior without your consent.", 
 
    "quoteAuthor": "Eleanor Roosevelt" 
 
    }, { 
 
    "quoteText": "One fails forward toward success.", 
 
    "quoteAuthor": "Charles Kettering" 
 
    }, { 
 
    "quoteText": "Argue for your limitations, and sure enough theyre yours.", 
 
    "quoteAuthor": "Richard Bach" 
 
    }, { 
 
    "quoteText": "Luck is what happens when preparation meets opportunity.", 
 
    "quoteAuthor": "Seneca" 
 
    }, { 
 
    "quoteText": "Victory belongs to the most persevering.", 
 
    "quoteAuthor": "Napoleon Bonaparte" 
 
    }, { 
 
    "quoteText": "Once you choose hope, anythings possible.", 
 
    "quoteAuthor": "Christopher Reeve" 
 
    }, { 
 
    "quoteText": "Love all, trust a few, do wrong to none.", 
 
    "quoteAuthor": "William Shakespeare" 
 
    }, { 
 
    "quoteText": "In order to win, you must expect to win.", 
 
    "quoteAuthor": "Richard Bach" 
 
    }, { 
 
    "quoteText": "A goal is a dream with a deadline.", 
 
    "quoteAuthor": "Napoleon Hill" 
 
    }, { 
 
    "quoteText": "You can do it if you believe you can!", 
 
    "quoteAuthor": "Napoleon Hill" 
 
    }, { 
 
    "quoteText": "Set your goals high, and don't stop till you get there.", 
 
    "quoteAuthor": "Bo Jackson" 
 
    }, { 
 
    "quoteText": "Genius is one percent inspiration and ninety-nine percent perspiration.", 
 
    "quoteAuthor": "Thomas Edison" 
 
    }, { 
 
    "quoteText": "Every new day is another chance to change your life.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "Smile, breathe, and go slowly.", 
 
    "quoteAuthor": "Thich Nhat Hanh" 
 
    }, { 
 
    "quoteText": "Nobody will believe in you unless you believe in yourself.", 
 
    "quoteAuthor": "Liberace" 
 
    }, { 
 
    "quoteText": "Be kind whenever possible. It is always possible.", 
 
    "quoteAuthor": "Dalai Lama" 
 
    }, { 
 
    "quoteText": "Do more than dream: work.", 
 
    "quoteAuthor": "William Arthur Ward" 
 
    }, { 
 
    "quoteText": "No man was ever wise by chance.", 
 
    "quoteAuthor": "Seneca" 
 
    }, { 
 
    "quoteText": "Some pursue happiness, others create it.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "It's easier to see the mistakes on someone else's paper.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "Think how hard physics would be if particles could think.", 
 
    "quoteAuthor": "Murray Gell-Mann" 
 
    }, { 
 
    "quoteText": "Well begun is half done.", 
 
    "quoteAuthor": "Aristotle" 
 
    }, { 
 
    "quoteText": "He that is giddy thinks the world turns round.", 
 
    "quoteAuthor": "William Shakespeare" 
 
    }, { 
 
    "quoteText": "Don't ruin the present with the ruined past.", 
 
    "quoteAuthor": "Ellen Gilchrist" 
 
    }, { 
 
    "quoteText": "Do something wonderful, people may imitate it.", 
 
    "quoteAuthor": "Albert Schweitzer" 
 
    }, { 
 
    "quoteText": "We do what we do because we believe.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "Great talent finds happiness in execution.", 
 
    "quoteAuthor": "Johann Wolfgang von Goethe" 
 
    }, { 
 
    "quoteText": "Do one thing every day that scares you.", 
 
    "quoteAuthor": "Eleanor Roosevelt" 
 
    }, { 
 
    "quoteText": "If you cannot be silent be brilliant and thoughtful.", 
 
    "quoteAuthor": "Byron Pulsifer" 
 
    }, { 
 
    "quoteText": "Smile, breathe, and go slowly.", 
 
    "quoteAuthor": "Thich Nhat Hanh" 
 
    }, { 
 
    "quoteText": "Who looks outside, dreams; who looks inside, awakes.", 
 
    "quoteAuthor": "Carl Jung" 
 
    }, { 
 
    "quoteText": "What we think, we become.", 
 
    "quoteAuthor": "Buddha" 
 
    }, { 
 
    "quoteText": "The shortest answer is doing.", 
 
    "quoteAuthor": "Lord Herbert" 
 
    }, { 
 
    "quoteText": "All our knowledge has its origins in our perceptions.", 
 
    "quoteAuthor": "Leonardo da Vinci" 
 
    }, { 
 
    "quoteText": "He is able who thinks he is able.", 
 
    "quoteAuthor": "Buddha" 
 
    }, { 
 
    "quoteText": "The harder you fall, the higher you bounce.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "Trusting our intuition often saves us from disaster.", 
 
    "quoteAuthor": "Anne Wilson Schaef" 
 
    }, { 
 
    "quoteText": "Truth is powerful and it prevails.", 
 
    "quoteAuthor": "Sojourner Truth" 
 
    }, { 
 
    "quoteText": "Talk doesn't cook rice.", 
 
    "quoteAuthor": "Chinese proverb" 
 
    }, { 
 
    "quoteText": "Light tomorrow with today!", 
 
    "quoteAuthor": "Elizabeth Browning" 
 
    }, { 
 
    "quoteText": "Silence is a fence around wisdom.", 
 
    "quoteAuthor": "German proverb" 
 
    }, { 
 
    "quoteText": "Society develops wit, but its contemplation alone forms genius.", 
 
    "quoteAuthor": "Madame de Stael" 
 
    }, { 
 
    "quoteText": "Real magic in relationships means an absence of judgement of others.", 
 
    "quoteAuthor": "Wayne Dyer" 
 
    }, { 
 
    "quoteText": "The years teach much which the days never know.", 
 
    "quoteAuthor": "Ralph Emerson" 
 
    }, { 
 
    "quoteText": "We can only learn to love by loving.", 
 
    "quoteAuthor": "Iris Murdoch" 
 
    }, { 
 
    "quoteText": "The simplest things are often the truest.", 
 
    "quoteAuthor": "Richard Bach" 
 
    }, { 
 
    "quoteText": "What you give is what you get.", 
 
    "quoteAuthor": "Byron Pulsifer" 
 
    }, { 
 
    "quoteText": "Everyone smiles in the same language.", 
 
    "quoteAuthor": "" 
 
    }, { 
 
    "quoteText": "A short saying often contains much wisdom.", 
 
    "quoteAuthor": "Sophocles" 
 
    }, { 
 
    "quoteText": "Yesterday I dared to struggle. Today I dare to win.", 
 
    "quoteAuthor": "Bernadette Devlin" 
 
    }, { 
 
    "quoteText": "Victory belongs to the most persevering.", 
 
    "quoteAuthor": "Napoleon Bonaparte" 
 
    }, { 
 
    "quoteText": "No alibi will save you from accepting the responsibility.", 
 
    "quoteAuthor": "Napoleon Hill" 
 
    }, { 
 
    "quoteText": "If you can dream it, you can do it.", 
 
    "quoteAuthor": "Walt Disney" 
 
    }, { 
 
    "quoteText": "From error to error one discovers the entire truth.", 
 
    "quoteAuthor": "Sigmund Freud" 
 
    }, { 
 
    "quoteText": "It is better to travel well than to arrive.", 
 
    "quoteAuthor": "Buddha" 
 
    }, { 
 
    "quoteText": "Life shrinks or expands in proportion to one's courage.", 
 
    "quoteAuthor": "Anais Nin" 
 
    }, { 
 
    "quoteText": "You have to believe in yourself.", 
 
    "quoteAuthor": "Sun Tzu" 
 
    }]; 
 

 
$.each(data, function (i, val) { 
 
    $("#items").append("<p>"+val.quoteText+"-"+val.quoteAuthor+"</p> "); 
 
});
<div id="items"> 
 
</div>

+0

查看複製我標記爲如何從數組中選擇一個隨機項。儘管如此,您應該注意,強制客戶端下載整個JSON文件只是爲了從中選擇一個項目,這是相當大的浪費時間。把你的服務器代碼寫成每個請求只返回一個單引號會更有意義。 –

+0

@RoryMcCrossan即使它看起來很浪費時間。但對我來說它有一些目的。但還是要感謝評論,參考和建議。 – Ailyn

回答

0

使用的Math.random選擇一個隨機的報價。

var i = Math.floor(Math.random() * data.length); 
var val = data[i]; 
$("#items").append("<p>"+val.quoteText+"-"+val.quoteAuthor+"</p> "); 
+0

對隨機數值工作良好。但仍然只需要一個值@Adder – Ailyn

+0

只有一個值是您的編程語言的一部分,您沒有向我們顯示任何信息,即如何生成'data'。 – Adder

相關問題