« [Json] Record化して処理する | トップページ | [Json]JSON Helper »

[Json] plist化して処理する

サンプルデータのように、『駅名』が何コ来るか想定出来ない場合
plistに保存してArrayの数を数えた方が簡単だったりますの。

ダウンロード - jsonc.zip



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions



###ファイルの場所のディレクトリを取得
tell application "Finder"
set aliasPathToMeDir to container of (path to me) as alias
set strPathToMeDir to POSIX path of aliasPathToMeDir as text
end tell
###JSONデータ『貴重なGETできる物』
set strURL to "http://express.heartrails.com/api/json?method=getStations&x=139.64277570210587&y=35.43546547173062"
##ファイル名をそれぞれ定義
set strPlistPath to "" & strPathToMeDir & "getStations.plist"
set strJsontPath to "" & strPathToMeDir & "getStations.json"
###JSONをダウンロードして
set strCommandText to "/usr/bin/curl -L -o '" & strJsontPath & "' '" & strURL & "' --connect-timeout 20" as text
do shell script strCommandText
###PLISTに変換して
set theCommandText to ("/usr/bin/plutil -convert xml1 \"" & strJsontPath & "\" -o \"" & strPlistPath & "\" ") as text
do shell script theCommandText

###システムイベント
tell application "System Events"
###plistを読み込む
tell property list file strPlistPath
every property list item
tell property list item "response"
tell property list item "station"
###戻り値を数えて
set numCntStation to count of every property list item
set numCntStNo to 1
###戻り値の数だけ繰り返し
repeat numCntStation times
tell property list item numCntStNo
tell property list item "name"
###駅名を読み込む
set strStationName to value
log strStationName
end tell
end tell
set numCntStNo to numCntStNo + 1
end repeat
end tell
end tell
end tell
end tell

--> "石川町"
--> "山手"
--> "関内"

|

« [Json] Record化して処理する | トップページ | [Json]JSON Helper »

Json」カテゴリの記事