Перейти к содержимому

Реферальная программа Мегаплана

Партнерская программа Kredov

Json вывод данных в таблицу

#1 go.to

go.to
  • Пользователь
  • 57 сообщений
  • Репутация: 6
0

Отправлено 06 Февраль 2014 - 15:33

Есть сайт который отдает данные в виде json. Необходимо спарсить оттуда первую десятку по количеству медалей, переменная "total".
Как это сделать ? Плюс помимо этого, в этом списке должна быть Россия, не зависимо от количества медалей.


Я сделал, но не то, что мне нужно.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru" xml:lang="ru">
<head>
<title>Пример преобразования данных в формате JSON из/в объект JavaScript</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="Gennady A. Samkov" />
<meta name="email" content="smkv@mail.ru" />
<meta name="domain" content="linkexchanger.su" />
<meta name="robots" content="noindex" />
<script type="text/javascript" src="jquery-latest.js"></script>
<script type="text/javascript" src="jquery.tablesorter.js"></script>
<style type="text/css">
th.header {
    background-image: url(/img/bg.gif);	
    cursor: pointer;
    font-weight: bold;
    background-repeat: no-repeat;
    background-position: center left; 
    margin-left: -1px;
}
* {
    margin:0;
    padding:0;
}
html, body {
    background-color:#E2F2E2;
    font-family: "Trebuchet MS", Tahoma, Verdana, Arial, Helvetica, sans-serif;
    font-size: 12pt;
}
table, th, td
{
border-style:solid;
border-color:black;
border-width:1px;
border-collapse:collapse;
text-align: center;
width: 700px
}
</style>
<script>
$(document).ready(function()
    {
	    $("#myTable").tablesorter();
    }
);
$(document).ready(function()
    {
	    $("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} );
    }
);
</script>

<script type="text/javascript">

var aAuto = [
  {
    "id": "AUS",
    "name": "Австралия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 12
  },
  {
    "id": "AUT",
    "name": "Австрия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 3
  },
  {
    "id": "AZE",
    "name": "Азербайджан",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 6
  },
  {
    "id": "ALB",
    "name": "Албания",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 9
  },
  {
    "id": "AND",
    "name": "Андорра",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ARG",
    "name": "Аргентина",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ARM",
    "name": "Армения",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "AFG",
    "name": "Афганистан",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "BLR",
    "name": "Беларусь",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "BEL",
    "name": "Бельгия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "BER",
    "name": "Бермуды",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "BUL",
    "name": "Болгария",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "BIH",
    "name": "Босния и Герцеговина",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "BRA",
    "name": "Бразилия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "IVB",
    "name": "Британские Виргинские острова ",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MKD",
    "name": "Бывшая Югославская Республика Македония",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "HUN",
    "name": "Венгрия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "VEN",
    "name": "Венесуэла",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ISV",
    "name": "Виргинские острова (США)",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "GER",
    "name": "Германия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "HKG",
    "name": "Гонконг",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "GRE",
    "name": "Греция",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "GEO",
    "name": "Грузия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "DEN",
    "name": "Дания",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "DMA",
    "name": "Доминика",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ZIM",
    "name": "Зимбабве",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ISR",
    "name": "Израиль",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "IRI",
    "name": "ИР Иран",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "IRL",
    "name": "Ирландия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ISL",
    "name": "Исландия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ESP",
    "name": "Испания",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ITA",
    "name": "Италия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "KAZ",
    "name": "Казахстан",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "CAN",
    "name": "Канада",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "CYP",
    "name": "Кипр",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "KGZ",
    "name": "Киргизия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "CHN",
    "name": "Китай",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "TPE",
    "name": "Китайский Тайбэй ",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "LAT",
    "name": "Латвия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "LIB",
    "name": "Ливан",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "LTU",
    "name": "Литва",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "LIE",
    "name": "Лихтенштейн",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "LUX",
    "name": "Люксембург",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MLT",
    "name": "Мальта",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MAR",
    "name": "Марокко",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MEX",
    "name": "Мексика",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MDA",
    "name": "Молдова",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MON",
    "name": "Монако",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MGL",
    "name": "Монголия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "IOP",
    "name": "Независимый Олимп. участник",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "NEP",
    "name": "Непал",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "NED",
    "name": "Нидерланды",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "NZL",
    "name": "Новая Зеландия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "NOR",
    "name": "Норвегия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "CAY",
    "name": "Острова Кайман",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "PAK",
    "name": "Пакистан",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "PAR",
    "name": "Парагвай",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "PER",
    "name": "Перу",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "POL",
    "name": "Польша",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "POR",
    "name": "Португалия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "RUS",
    "name": "Россия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "ROU",
    "name": "Румыния",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "SMR",
    "name": "Сан-Марино",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "SRB",
    "name": "Сербия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "SVK",
    "name": "Словакия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "SLO",
    "name": "Словения",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "GBR",
    "name": "Соед. Королевство",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "USA",
    "name": "США",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "TJK",
    "name": "Таджикистан",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "THA",
    "name": "Таиланд",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "TLS",
    "name": "Тимор-Лесте",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "TOG",
    "name": "Того",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "TGA",
    "name": "Тонга",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "TUR",
    "name": "Турция",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "UZB",
    "name": "Узбекистан",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "UKR",
    "name": "Украина",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "PHI",
    "name": "Филиппины",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "FIN",
    "name": "Финляндия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "FRA",
    "name": "Франция",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "CRO",
    "name": "Хорватия",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "MNE",
    "name": "Черногория",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "CZE",
    "name": "Чешская Республика",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "CHI",
    "name": "Чили",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "SUI",
    "name": "Швейцария",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "SWE",
    "name": "Швеция",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "EST",
    "name": "Эстония",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "KOR",
    "name": "Южная Корея",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "JAM",
    "name": "Ямайка",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  },
  {
    "id": "JPN",
    "name": "Япония",
    "rank": 0,
    "rankStr": "-",
    "gold": 0,
    "silver": 0,
    "bronze": 0,
    "total": 0
  }
];

var aAuto = JSON.stringify(aAuto);
document.write('<hr />' );
var aAuto = JSON.parse(aAuto);
document.write
('<table id="myTable" class="tablesorter" style="margin-left: 350px; margin-top:300px">' + '<thead><tr><th>'+ "Страна" + '</th><th>' + "Ранк" + '</th><th>' + "Хер знает что" + '</th><th>' + "Золото" + '</th><th>' + "Серебро" + '</th><th>' + "Бронза" + '</th><th>' + "Всего" + '</th><tr></thead><tbody>' +
'<tr><td>'+aAuto[0].name + '</td><td>' + aAuto[0].rank + '</td><td>' + aAuto[0].rankStr + '</td><td>' + aAuto[0].gold + '</td><td>' + aAuto[0].silver + '</td><td>' + aAuto[0].bronze + '</td><td>' + aAuto[0].total + '</td><tr>' +
'<tr><td>'+aAuto[1].name + '</td><td>' + aAuto[1].rank + '</td><td>' + aAuto[1].rankStr + '</td><td>' + aAuto[1].gold + '</td><td>' + aAuto[1].silver + '</td><td>' + aAuto[1].bronze + '</td><td>' + aAuto[1].total + '</td><tr>' +
'<tr><td>'+aAuto[2].name + '</td><td>' + aAuto[2].rank + '</td><td>' + aAuto[2].rankStr + '</td><td>' + aAuto[2].gold + '</td><td>' + aAuto[2].silver + '</td><td>' + aAuto[2].bronze + '</td><td>' + aAuto[2].total + '</td><tr>' +
'<tr><td>'+aAuto[3].name + '</td><td>' + aAuto[3].rank + '</td><td>' + aAuto[3].rankStr + '</td><td>' + aAuto[3].gold + '</td><td>' + aAuto[3].silver + '</td><td>' + aAuto[3].bronze + '</td><td>' + aAuto[3].total + '</td><tr>' +
'<tr><td>'+aAuto[4].name + '</td><td>' + aAuto[4].rank + '</td><td>' + aAuto[4].rankStr + '</td><td>' + aAuto[4].gold + '</td><td>' + aAuto[4].silver + '</td><td>' + aAuto[4].bronze + '</td><td>' + aAuto[4].total + '</td><tr>' +
'<tr><td>'+aAuto[5].name + '</td><td>' + aAuto[5].rank + '</td><td>' + aAuto[5].rankStr + '</td><td>' + aAuto[5].gold + '</td><td>' + aAuto[5].silver + '</td><td>' + aAuto[5].bronze + '</td><td>' + aAuto[5].total + '</td><tr>' +
'<tr><td>'+aAuto[6].name + '</td><td>' + aAuto[6].rank + '</td><td>' + aAuto[6].rankStr + '</td><td>' + aAuto[6].gold + '</td><td>' + aAuto[6].silver + '</td><td>' + aAuto[6].bronze + '</td><td>' + aAuto[6].total + '</td><tr>' +
'<tr><td>'+aAuto[7].name + '</td><td>' + aAuto[7].rank + '</td><td>' + aAuto[7].rankStr + '</td><td>' + aAuto[7].gold + '</td><td>' + aAuto[7].silver + '</td><td>' + aAuto[7].bronze + '</td><td>' + aAuto[7].total + '</td><tr>' +
'<tr><td>'+aAuto[8].name + '</td><td>' + aAuto[8].rank + '</td><td>' + aAuto[8].rankStr + '</td><td>' + aAuto[8].gold + '</td><td>' + aAuto[8].silver + '</td><td>' + aAuto[8].bronze + '</td><td>' + aAuto[8].total + '</td><tr>' +
'<tr><td>'+aAuto[9].name + '</td><td>' + aAuto[9].rank + '</td><td>' + aAuto[9].rankStr + '</td><td>' + aAuto[9].gold + '</td><td>' + aAuto[9].silver + '</td><td>' + aAuto[9].bronze + '</td><td>' + aAuto[9].total + '</td><tr>' +
"</tbody></table>");

</script>


1) нужно чтобы данные брались с сервера а не из документа
2) нужно сначала остортировать 10ку лучших по медалям. а потом вывести

Помогите :)

 

 

  • 0

#2 Ixman

Ixman
  • Пользователь PRO
  • 2 159 сообщений
  • Репутация: 432

Отправлено 06 Февраль 2014 - 16:28

json_decode(); а затем уже функции для работы с массивами, сортируйте как хотите и в цикле выбираете то что нужно
  • 0

#3 go.to

go.to
    Topic Starter
  • Пользователь
  • 57 сообщений
  • Репутация: 6

Отправлено 07 Февраль 2014 - 09:11

Подробнее можно?

Пробовал джсон_декоде, он даже не парсит эту страницу скорее всего из-за того что она не в формате .json
  • 0


Оформление форума – IPBSkins.ru