lotusscript 中的 NotesJSONNavigator 问题

issue with NotesJSONNavigator in lotusscript

我有以下代理来测试新的 V10 JSON 解析器 代码中的json是从darksky weather api

中检索出来的
Option Public
Option Declare

Sub Initialize
    Dim json As String
    Dim session As New NotesSession

    json = |{
"latitude": 51.2747748,
"longitude": 4.4433923,
"timezone": "Europe/Brussels",
"daily": {
    "summary": "Rain today, with high temperatures falling to 3øC next Sunday.",
    "icon": "rain",
    "data": [{
        "time": 1547334000,
        "summary": "Rain in the afternoon and breezy starting in the afternoon.",
        "icon": "rain",
        "sunriseTime": 1547365378,
        "sunsetTime": 1547395251,
        "moonPhase": 0.23,
        "precipIntensity": 0.4115,
        "precipIntensityMax": 1.5621,
        "precipIntensityMaxTime": 1547380800,
        "precipProbability": 0.97,
        "precipType": "rain",
        "temperatureHigh": 10.56,
        "temperatureHighTime": 1547391600,
        "temperatureLow": 5.5,
        "temperatureLowTime": 1547449200,
        "apparentTemperatureHigh": 10.56,
        "apparentTemperatureHighTime": 1547391600,
        "apparentTemperatureLow": 2.06,
        "apparentTemperatureLowTime": 1547427600,
        "dewPoint": 6.77,
        "humidity": 0.87,
        "pressure": 1009.48,
        "windSpeed": 7.24,
        "windGust": 17.26,
        "windGustTime": 1547395200,
        "windBearing": 285,
        "cloudCover": 0.93,
        "uvIndex": 1,
        "uvIndexTime": 1547377200,
        "visibility": 12.59,
        "ozone": 311.57,
        "temperatureMin": 7.17,
        "temperatureMinTime": 1547416800,
        "temperatureMax": 10.56,
        "temperatureMaxTime": 1547391600,
        "apparentTemperatureMin": 2.64,
        "apparentTemperatureMinTime": 1547416800,
        "apparentTemperatureMax": 10.56,
        "apparentTemperatureMaxTime": 1547391600
    }]
},
"offset": 1
}|

    json = removeCRLF(json)
    Dim jsnav As NotesJSONNavigator 
    Set jsnav = session.CreateJSONNavigator(json)
    Dim el As NOTESJSONELEMENT
    Set el = jsnav.getelementbypointer("/latitude")
    Print CStr(el.value)
End Sub
Function removeCRLF(json) As String
    removeCRLF =Replace(Replace(json, Chr(13), ""),Chr(10),"")
End Function

我在 运行 代理时收到此错误:

Unable to Parse JSON string: Missing a comma or '}' after an object member. offset 1791

经过一些测试,我发现错误来自 json 中的一个特殊字符(ø in '... falling to 3øC next ...')。

任何人都可以帮助我了解如何 avoid/convert 在解析 JSON 时可能导致问题的字符吗?

PS:openntf JSON 解析器正确处理 json。

这一定是一个字符集问题,果然...有一个 属性 似乎是相关的。检查 jsnav.PreferUTF8 的值。它记录在这里:link。他们没有说默认值是什么。如果为真,则将其设置为假。如果为假,则将其设置为真。

如果您可以将 JSON 保存到文件系统中,您应该能够以 UTF-8 格式读取它,然后可以使用 JSONNav 获取您要查找的值.

Sub Initialize
    Dim json As Variant
    Dim session As New NotesSession
    Dim inbuf As NotesStream
    Dim path As String

    path = "c:\jsontest.json"  
    Set inbuf = session.Createstream()
    If Not(inbuf.Open(path, "UTF-8")) Then
        Print "Unable to open JSON file"
        Exit Sub
    End If
    json = inbuf.Read()

    If IsArray(json) then
        Dim jsnav As NotesJSONNavigator
        Set jsnav = session.CreateJSONNavigator(json)
        Dim el As NotesJSONElement
        Set el = jsnav.getelementbypointer("/latitude")
        Print CStr(el.value)
    Else
        Print "JSON is nothing"
    End If
End Sub

正在努力获得有关如何使用内联实现的更好答案JSON。

10.0.1 FP2 已修复此问题,现在可供下载:https://www-01.ibm.com/support/docview.wss?uid=ibm10871936. You’ll want to use the new PreferJSONNavigator property on the request. See this technote for details https://www-01.ibm.com/support/docview.wss?uid=ibm10875724

看来您可以使用 NotesMIMEEntity 进行 UTF-8 转换来解决此问题,这可以在内存中完成,并且无需诉诸文件系统调用。

Sub Initialize
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim stream As NotesStream
    Dim doc As NotesDocument
    Dim jsNav As NotesJSONNavigator
    Dim mime As NotesMIMEEntity
    Dim js$

    js = |{"summary": "Rain today, with high temperatures falling to 3øC next Sunday."}|

    Set db = session.CurrentDatabase
    Set doc = db.CreateDocument
    Set mime = doc.CreateMIMEEntity
    Set stream = session.CreateStream

    ' Write the JSON to a stream
    Call stream.WriteText(js)

    ' Write the stream into a NotesMIMEEntity with UTF-8 character set specified
    Call mime.SetContentFromText(stream, {text/plain;charset="UTF-8"}, ENC_NONE)

    ' Clear the existing stream and read the NotesMIMEEntity contents back in as binary
    Call stream.Truncate
    Call mime.GetContentAsBytes(stream)

    ' Finally read in the corrected JSON
    stream.Position = 0
    Set jsNav = session.CreateJSONNavigator(stream.Read)
    Call stream.Close

    MsgBox jsNav.GetFirstElement.Value
End Sub