You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

B4J代码本地运行正常,部署到服务器报JSON Object expected错误

问题排查:B4J部署服务器后JSON解析报错JSON Object expected

问题场景

本地运行B4J代码完全正常,但部署到服务器后触发java.lang.RuntimeException: java.lang.RuntimeException: JSON Object expected异常。本地环境可正常读取并处理endpoints.json文件,服务器环境下却触发该异常。

相关代码

JSON读取处理代码

Sub tickInterval_tick
    time.Enabled = False
    Dim endpointList As List
    Dim endpointMap As Map
    endpointList = File.ReadList(File.DirApp, "endpoints.json")
    Dim filePath As String = File.Combine(File.DirApp, "endpoints.json")
    Log("File path: " & filePath)
    Dim parser As JSONParser
    endpointMap.Initialize
    parser.Initialize(endpointList.Get(0))
    Dim endpointArray As List = parser.NextArray
    For t = 0 To endpointArray.Size - 1
        endpointMap = endpointArray.Get(t)
        ContainerReady(endpointMap.Get("name"), endpointMap.Get("host"), endpointMap.Get("token"))
    Next
    time.Enabled= True
End Sub

endpoints.json内容

[{"host":"https://xxxx:xxxx:xxxx","name":"OKD 1","token":"Bearer xxxxxxxxxx"}]

完整报错堆栈

java.lang.RuntimeException: java.lang.RuntimeException: JSON Object expected.
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:140)
        at anywheresoftware.b4a.BA$3.run(BA.java:267)
        at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
        at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
        at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
        at b4j.example.main._appstart(main.java:85)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:577)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
        at b4j.example.main.main(main.java:28)
Caused by: java.lang.RuntimeException: JSON Object expected.
        at anywheresoftware.b4j.objects.collections.JSONParser.NextObject(JSONParser.java:67)
        at b4j.example.main._shcurl_processcompleted(main.java:424)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)

排查分析

  1. 文件读取逻辑缺陷:当前用File.ReadList按行读取JSON文件,若服务器上的endpoints.json存在换行、空白行或编码差异,endpointList.Get(0)可能无法获取完整的JSON数组内容,导致解析失败。
  2. 路径与权限问题:服务器上File.DirApp指向的目录可能不存在endpoints.json,或程序无文件读取权限,造成读取内容为空或错误。
  3. 异常根源指向:注意到堆栈中异常实际来自_shcurl_processcompleted方法,而非tickInterval_tick的本地JSON读取——说明问题大概率出在ContainerReady调用的shcurl进程返回结果的解析环节:本地环境中shcurl返回正常JSON对象,但服务器上返回内容不符合预期(比如空响应、非JSON格式的错误页)。

解决方法

优化本地JSON文件读取

  • 改用File.ReadString直接读取整个文件内容,避免按行读取的潜在问题:
Sub tickInterval_tick
    time.Enabled = False
    Dim endpointMap As Map
    Dim filePath As String = File.Combine(File.DirApp, "endpoints.json")
    Log("File path: " & filePath)
    ' 直接读取完整文件内容
    Dim jsonContent As String = File.ReadString(File.DirApp, "endpoints.json")
    Dim parser As JSONParser
    endpointMap.Initialize
    parser.Initialize(jsonContent)
    Dim endpointArray As List = parser.NextArray
    For t = 0 To endpointArray.Size - 1
        endpointMap = endpointArray.Get(t)
        ContainerReady(endpointMap.Get("name"), endpointMap.Get("host"), endpointMap.Get("token"))
    Next
    time.Enabled= True
End Sub
  • 验证服务器文件状态:确认endpoints.json存在于File.DirApp指向的目录,且程序拥有读取权限;检查文件编码与本地一致(推荐UTF-8无BOM格式)。

排查shcurl_processcompleted环节

  • 在该方法中添加日志,输出原始返回内容,确认是否为有效JSON对象:
Sub shcurl_processcompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log("shcurl stdout: " & StdOut)
    Log("shcurl stderr: " & StdErr)
    If Success And ExitCode = 0 Then
        ' 先校验返回内容有效性
        If StdOut.Trim.Length = 0 Then
            Log("shcurl返回内容为空")
            Return
        End If
        Try
            Dim parser As JSONParser
            parser.Initialize(StdOut)
            Dim resultMap As Map = parser.NextObject
            ' 后续业务逻辑
        Catch
            Log("解析shcurl返回JSON失败: " & LastException.Message)
        End Try
    Else
        Log("shcurl执行失败,错误码: " & ExitCode)
    End If
End Sub
  • 检查服务器shcurl执行环境:确认目标接口可访问、请求参数正确,返回的是预期的JSON对象而非HTML错误页、空响应等非预期内容。

内容的提问来源于stack exchange,提问作者Ardean Angkasa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 23:57:22