2014年4月15日 星期二

Domino Notes 7.0 透過web API撈Json資料的範例

取網頁資料請參考這一篇(Notes Client只能在Windows平台上操作喔)

http://stackoverflow.com/questions/12719192/get-a-field-value-from-a-webpage-lotusscript

取json則請使用這個ntf建好的script程式庫

http://www.openntf.org/main.nsf/project.xsp?r=project/JSON%20LS

請先把用到的Class先準備好

這個例子是做當人事單位按開始審核按鈕時,系統自動透過webAPI取回相關打卡紀錄存入PunchCards欄位做佐證.

我放在Form的Postopen事件

Sub Postopen(Source As Notesuidocument)
If source.EditMode Then
Dim internet As New RemoteHTML()
Dim html As String
Dim sJSON As String
Dim PunchTime As String
Dim jsonReader As JSONReader
Dim vResults As Variant
Set jsonReader = New JSONReader
Set doc=uidoc.Document
url$="http://10.100.2.1:8080/API/Employees/"
url2$="/"+Format$(doc.STime(0),"yyyymmdd")+"/"+Format$(doc.ETime(0),"yyyymmdd")
url1$=url$+Ucase(Trim(doc.EmployeeID(0)))+url2$
html = internet.GetHTTP(url1$)
Set vResults = jsonReader.Parse(html) 'this is a JSONObject
If  vResults.Count > 0 Then
PunchTime = PunchTime +Chr(13) + Chr(10)+ vResults.items(0).GetItemValue("RSENO") + vResults.items(0).GetItemValue("BPNME")
For iV = 1 To  vResults.Count
PunchTime = PunchTime +Chr(13) + Chr(10) + "                   "+FindReplace(vResults.items(iv-1).GetItemValue("RSNOW"),"T", "   ")
Next
End If
Dim m_item As NotesItem
Set m_item=doc.GetFirstItem("ApplyListID")
If Not m_item  Is Nothing Then
Forall x In m_item.Values
If Len(Ucase(Cstr(x))) > 0 Then
url1$=url$+Ucase(Cstr(x))+url2$
html = internet.GetHTTP(url1$)
Set vResults = jsonReader.Parse(html) 'this is a JSONObject
If  vResults.Count > 0 Then
PunchTime = PunchTime +Chr(13) + Chr(10)+ vResults.items(0).GetItemValue("RSENO") + vResults.items(0).GetItemValue("BPNME")
For iV = 1 To  vResults.Count
PunchTime = PunchTime +Chr(13) + Chr(10)+ "                   "+FindReplace(vResults.items(iv-1).GetItemValue("RSNOW") , "T", "   ")
Next
End If
End If
End Forall
End If
Call Source.FieldSetText("PunchCards",PunchTime)
End If
End Sub

WebAPI Help

http://10.100.2.1:8080/API/Employees/ID/DATE1/DATE2

ID: 卡號 (EX:K1116)
DATE1 :日期 (EX:20140101)
DATE 2:日期 (EX:20140131)


傳回json範例

[{"RSENO":"K1116","RSTME":"07:53","RSDAT":"2014-04-01T00:00:00","RSNOW":"2014-04-01T07:53:00","BPNME":"Jeff","BPEML":"Jeff@adimxxne.com.tw"},{"RSENO":"K1116","RSTME":"17:24","RSDAT":"2014-04-01T00:00:00","RSNOW":"2014-04-01T17:24:00","BPNME":"Jeff","BPEML":"Jeff@adimxxne.com.tw"}

沒有留言:

張貼留言