จากตอนที่แล้ว Reverse Geocoding (Address Lookup) ผ่าน http
เรื่องวันนี้ ! ให้สังเกตว่า
เมื่อเรา Request ไปข้อข้อมูลของ Thammasat Rangsit เราจะได้ ข้อมูลหน้าตาประมาณนี้
{ "name": "Thammasat Rangsit", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "Thammasat Rangsit Sport Center, Khlong Nueng, Khlong Luang, Pathum Thani 12110, Thailand", "AddressDetails": { "Accuracy" : 9, "AddressLine" : [ "Thammasat Rangsit Sport Center" ] }, "ExtendedData": { "LatLonBox": { "north": 14.0785781, "south": 14.0569314, "east": 100.6173654, "west": 100.5853506 }
}, "Point": { "coordinates": [ 100.6013580, 14.0677550, 0 ] } }, { "id": "p2", "address": "Thammasat Rangsit Sport Center, Khlong Nueng, Khlong Luang, Pathum Thani 12110, Thailand", "AddressDetails": { "Accuracy" : 9, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "Pathum Thani", "Locality" : { "AddressLine" : [ "Thammasat Rangsit Sport Center" ], "LocalityName" : "Khlong Nueng", "PostalCode" : { "PostalCodeNumber" : "12110" } } }, "CountryName" : "Thailand", "CountryNameCode" : "TH" } }, "ExtendedData": { "LatLonBox": { "north": 14.0702524, "south": 14.0639572, "east": 100.6053812, "west": 100.5990860 } }, "Point": { "coordinates": [ 100.6022336, 14.0671048, 0 ] } } ] }
ลองดูดีๆ เราจะเห็นว่า output ที่ google ตอบกลับมามันมีรูปแบบ !
ซึ่งข้อมูลแบบนี้เค้าเรียกว่า JSON
เมื่อลองดูที่ข้อมูล จะเห็นว่า เรามี "id": "p2" กับ "id": "p1" (มี field id เหมือนกัน )
นั่นก็แปลว่าเรามีข้อมูลสอง Record หรือ สอง Tuple (ให้นึกถึง Database มีฟิลด์เหมือนกัน แต่ข้อมูลต่างกัน)
เมื่อพูดถึงการ Request แบบปกติไปแล้ว (เข้าผ่าน Browser)
คราวนี้ลองเขียน Grails ให้่ไป Request ข้อมูลมาหน่อย ซึ่งก็ทำได้โคตรง่ายดังนี้
- create controller ขึ้นมาตัวนึง (สั่ง $ grails create-controller Test)
- import grails.converters.*;
- เขียน Code ใน action ได้เลย !
ซึ่่งมันจะได้ code แบบนี้
import grails.converters.*;
class TestController {
def index = {
def url = new URL("http://maps.google.com/maps/geo?q=Thammasat+Rangsit&output=json&sensor=false&gl=th")
def response = JSON.parse(url.newReader())
// response is an instance of JSONObject (see Grails API docs)
println response.toString(3) // Pretty-printed output
}
}
จากนั้นเราลองเรียกไปที่ Test Controller , action index ( http://cstis.selfip.com:8080/yourapp/test/index )
ดูที่ shell เราจะได้ output มาประมาณนี้
โอ้ว Output เหมือนกันเด๊ะ
ปัญหาต่อไปก็คือ เราจะไปดึงค่าในนั้นออกมาใช้ยังไงล่ะ ?
ลองรัน Code นี้ดู
response.each { key, value ->
println "key -> "+ key
/*println "$key := $value "*/
}
จะเห็นว่า เราได้ Key มา 3 อัน คือ
key -> Status
key -> name
key -> Placemark
ถ้าอยากรู้ว่ามันเก็บอะไร ก็ลอง print $value ดู
คราวนี้ ใช้งานจริง !
ถ้าเราสนใจพิกัดก็มองไป ตรง
"Point": {
"coordinates": [ 100.6022336, 14.0671048, 0 ]
}
จะเห็นว่ามันอยู่ใต้ Placemark เราสามารถเข้าถึงมันได้ โดย code
render response.Placemark.Point.coordinates
เมื่อลองรันดู จะได้ [[100.601358, 14.067755, 0], [100.6022336, 14.0671048, 0]] ( 2 Records )
code จาก http://stackoverflow.com/questions/1323758/json-in-groovy-grails




