여기에 총 자바 스크립트 멍청이. 언어에 대한 이해를 얻으려는 것뿐입니다.
다음 코드를 사용하여 JSON 요청을 요청합니다.
function request(){
$.ajax({
dataType: "jsonp",
type: 'GET',
url: "getWebsite",
success: function(result){
data = result;
$('.data').text(data);
console.log(data);
}});
get 요청은 다음과 같은 내용을 반환합니다.
"items": [
{
"topLevelComment": {
"authorDisplayName": "a"
"textDisplay": "b"
},
{
"topLevelComment": {
"authorDisplayName": "c"
"textDisplay": "d"
}
AuthorDisplayName과 textDisplay를 순환하고 각각에서 무작위로 하나를 선택하고 싶습니다. 이를 수행하는 가장 좋은 방법은 추측해야 할 경우 둘 다 배열에 넣는 것입니다. 어떻게해야할지 모르겠습니다.
데이터가 이미 개체 형식이고 임의 선택에서만 가져 오려는 경우. 그러면 모든 데이터를 반복 할 필요가 없습니다. 전체 데이터에서 하나의 인덱스를 무작위로 선택하십시오.
function request(){
$.ajax({
dataType: "jsonp",
type: 'GET',
url: "getWebsite",
success: function(result){
data = result;
$('.data').text(data);
console.log(data);
var randomIndex = Math.floor((Math.random() * data.items.length));
console.log("Selected data authorDisplayName: " + data.items[randomIndex].topLevelComment.authorDisplayName);
console.log("Selected data textDisplay: " + data.items[randomIndex].topLevelComment.textDisplay);
}
});
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다