Changes for page 2 Script
Last modified by Devin Chen on 2025/06/06 14:03
Summary
-
Page properties (1 modified, 0 added, 0 removed)
-
Attachments (0 modified, 1 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -349,6 +349,105 @@ 349 349 end 350 350 {{/code}} 351 351 352 + 353 +== **1.8 How to parse value from HTTP response body** == 354 + 355 +This example use [[https:~~/~~/www.weatherapi.com/>>https://www.weatherapi.com/]] as example, to show how to parse value from HTTP response body. When we input the city name into address "@HDW5050": 356 + 357 +(% style="text-align:center" %) 358 +[[image:InputHTTPparameter.png]] 359 + 360 +Then the response body would be like as following: 361 + 362 +{{code language="json"}} 363 +{ 364 + "location": { 365 + "name": "Madrid", 366 + "region": "Madrid", 367 + "country": "Spain", 368 + "lat": 40.4, 369 + "lon": -3.68, 370 + "tz_id": "Europe/Madrid", 371 + "localtime_epoch": 1669022636, 372 + "localtime": "2022-11-21 10:23" 373 + }, 374 + "current": { 375 + "last_updated_epoch": 1669022100, 376 + "last_updated": "2022-11-21 10:15", 377 + "temp_c": 13.0, 378 + "temp_f": 55.4, 379 + "is_day": 1, 380 + "condition": { 381 + "text": "Partly cloudy", 382 + "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png", 383 + "code": 1003 384 + }, 385 + "wind_mph": 11.9, 386 + "wind_kph": 19.1, 387 + "wind_degree": 210, 388 + "wind_dir": "SSW", 389 + "pressure_mb": 1015.0, 390 + "pressure_in": 29.97, 391 + "precip_mm": 0.0, 392 + "precip_in": 0.0, 393 + "humidity": 88, 394 + "cloud": 75, 395 + "feelslike_c": 10.8, 396 + "feelslike_f": 51.4, 397 + "vis_km": 10.0, 398 + "vis_miles": 6.0, 399 + "uv": 3.0, 400 + "gust_mph": 22.1, 401 + "gust_kph": 35.6 402 + } 403 +} 404 +{{/code}} 405 + 406 +(% class="wikigeneratedid" %) 407 +So we decode json into lua object to assign the value into addresses HDW6060(temperature), HDW7070(humidity) the code example like follows: 408 + 409 +{{code language="lua"}} 410 +local APIkey = '70faaecf926b4341b1974006221711' 411 + 412 + 413 +local http = require("socket.http") 414 +local json = require("json") 415 + 416 +-- Send http.get request and return response result 417 +function getHttpsUrl(url) 418 + local result_table, code, headers, status = http.request(url) 419 + print("code:"..code) 420 + if code~= 200 then 421 + return 422 + else 423 + return result_table 424 + end 425 +end 426 + 427 +function sendAPI(key, city) 428 + local url = "http://api.weatherapi.com/v1/current.json?key="..key.."&q="..city.."&aqi=no" 429 + --local url = 'http://v-box.net' 430 + --local url = 'https://www.google.com/' 431 + --http://api.weatherapi.com/v1/current.json?key=70faaecf926b4341b1974006221711&q=Barcelona&aqi=no 432 + print("Get the link:"..url) 433 + local body = getHttpsUrl(url) 434 + --print(body) 435 + local jsonBody = json.decode(body) 436 + --print(jsonBody["current"]["temp_c"]) 437 + --print(type(jsonBody["current"]["temp_c"])) 438 + --print(type(jsonBody["current"]["humidity"])) 439 + addr_setfloat("@HDW6060", jsonBody["current"]["temp_c"]) 440 + addr_setword("@HDW7070", jsonBody["current"]["humidity"]) 441 +end 442 + 443 + 444 +function Weather.main() 445 + local cityName = addr_getstring("@HDW5050",6) 446 + print("cityName: "..cityName) 447 + sendAPI(APIkey, cityName) 448 +end 449 +{{/code}} 450 + 352 352 = **2 V-Box connect with third part server** = 353 353 354 354 V-Box have two mode.One is for V-Net,User need to use WECON server to store data.We call this V-NET platform.
- InputHTTPparameter.png
-
- Author
-
... ... @@ -1,0 +1,1 @@ 1 +XWiki.Hunter - Size
-
... ... @@ -1,0 +1,1 @@ 1 +17.9 KB - Content