Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]JSON - pobieranie danych
olo707
post 7.08.2013, 18:20:09
Post #1





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 30.03.2012

Ostrzeżenie: (0%)
-----


Witam

Muszę pobrać dane ze strony



http://free.worldweatheronline.com/feed/we...9d6153629131202



na chwilę obecną korzystam z następującego skryptu

Kod
$(document).ready(function() {
$.getJSON("weather.json", function(data){
       $.each(data.data.current_condition, function(){
             $("ul").append("<li>pressure: "+this['pressure']+"</li><br /><li>temp_C: "+this['temp_C']+"</li><br /><li>temp_F: "+this['temp_F']+"</li><br /><li>zachmurzenie: "+this['weatherDesc.value']+"</li>");
       });
});
  });


ale dzięki niemu mogę się dobrać tylko do pierwszego poziomu danych np current_condition": [ {"cloudcover": "0", "humidity": "74"
czyli tylko do np cloudcover i humidity, nie mogę się dobrać do drugiego poziomu danych (tablicy asocjacyjnej). Próbowałem kombinować
pisząc w nawiasie po append np this['pressure']['cos tam'] ale nic to nie daje.

Mam jeszcze pytanie odnośnie pobierania adresu url (jest to w linku który podałem powyżej) pod którym jest umieszczone zdjęcie czy zdjęcie te wyświetli się automatycznie czy używa się jakiejś funkcji aby to osiągnąć

Go to the top of the page
+Quote Post
lobopol
post 7.08.2013, 19:08:29
Post #2





Grupa: Zarejestrowani
Postów: 1 729
Pomógł: 346
Dołączył: 4.04.2009

Ostrzeżenie: (0%)
-----


na stronie którą podałeś jest taka struktura danych:
[JAVASCRIPT] pobierz, plaintext
  1. {
  2. "data": {
  3. "current_condition": [{
  4. "cloudcover": "0",
  5. "humidity": "69",
  6. "observation_time": "06:01 PM",
  7. "precipMM": "0.0",
  8. "pressure": "1014",
  9. "temp_C": "23",
  10. "temp_F": "73",
  11. "visibility": "10",
  12. "weatherCode": "113",
  13. "weatherDesc": [{
  14. "value": "Sunny"
  15. }],
  16. "weatherIconUrl": [{
  17. "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
  18. }],
  19. "winddir16Point": "NE",
  20. "winddirDegree": "40",
  21. "windspeedKmph": "6",
  22. "windspeedMiles": "4"
  23. }],
  24. "request": [{
  25. "query": "Gdansk, Poland",
  26. "type": "City"
  27. }],
  28. "weather": [{
  29. "date": "2013-08-07",
  30. "precipMM": "0.5",
  31. "tempMaxC": "30",
  32. "tempMaxF": "85",
  33. "tempMinC": "20",
  34. "tempMinF": "68",
  35. "weatherCode": "176",
  36. "weatherDesc": [{
  37. "value": "Patchy rain nearby"
  38. }],
  39. "weatherIconUrl": [{
  40. "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"
  41. }],
  42. "winddir16Point": "S",
  43. "winddirDegree": "175",
  44. "winddirection": "S",
  45. "windspeedKmph": "12",
  46. "windspeedMiles": "7"
  47. }, {
  48. "date": "2013-08-08",
  49. "precipMM": "0.0",
  50. "tempMaxC": "34",
  51. "tempMaxF": "92",
  52. "tempMinC": "18",
  53. "tempMinF": "65",
  54. "weatherCode": "113",
  55. "weatherDesc": [{
  56. "value": "Sunny"
  57. }],
  58. "weatherIconUrl": [{
  59. "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
  60. }],
  61. "winddir16Point": "SSE",
  62. "winddirDegree": "159",
  63. "winddirection": "SSE",
  64. "windspeedKmph": "18",
  65. "windspeedMiles": "11"
  66. }, {
  67. "date": "2013-08-09",
  68. "precipMM": "0.1",
  69. "tempMaxC": "23",
  70. "tempMaxF": "73",
  71. "tempMinC": "16",
  72. "tempMinF": "61",
  73. "weatherCode": "116",
  74. "weatherDesc": [{
  75. "value": "Partly Cloudy"
  76. }],
  77. "weatherIconUrl": [{
  78. "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"
  79. }],
  80. "winddir16Point": "WNW",
  81. "winddirDegree": "300",
  82. "winddirection": "WNW",
  83. "windspeedKmph": "18",
  84. "windspeedMiles": "11"
  85. }]
  86. }
  87. }
[JAVASCRIPT] pobierz, plaintext

jeżeli chcesz się dostać do np. weather desc to musisz dla tego parametru znowu eacha zrobić bo jest to tablica, zobacz, że te json ma pozagłębiane kolejne tablice w sobie


--------------------
Go to the top of the page
+Quote Post
olo707
post 7.08.2013, 20:31:08
Post #3





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 30.03.2012

Ostrzeżenie: (0%)
-----


Dlaczego nie działa pisząc np w ten sposób this['weatherDesc']['value']
Go to the top of the page
+Quote Post
lobopol
post 7.08.2013, 20:32:48
Post #4





Grupa: Zarejestrowani
Postów: 1 729
Pomógł: 346
Dołączył: 4.04.2009

Ostrzeżenie: (0%)
-----


No bo przecież Ci napisałem, że weatherDesc to tablica this['weatherDesc'][0]['value']


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 8.07.2025 - 10:01