首页 > 解决方案 > hudson.remoting.ProxyException:net.sf.json.JSONException:无效的 JSON 字符串

问题描述

尝试在 Jenkins 中解析 json 文件时遇到以下错误

def appconfig="./appSettings.json"
def testconfig= readJSON file: appconfig;

json文件

{
  "Configuration": {
    "ConnectionString": "",
    "ConnectionString2": "",
    "UnProcessedHeaderFilePath": "\\\\edisilon01\\prod\\Reporting\\GET\\Header-{0:yyyyMMddHHmmss}.txt",
    "UnProcessedDetailFilePath": "\\\\edisilon01\\prod\\Reporting\\GET\\Detail-{0:yyyyMMddHHmmss}.txt",
    "ApplicationTriggerFile": "\\\\edisilon01\\PROD\\Reporting\\InputFiles\\Factor\\DR\\Factor.txt",
    "DB2ConnectionString": ""
  },
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Information",
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "path": "\\\\ECPCICS\\Files\\Generator\\log\\log-.txt",
          "rollingInterval": "Day",
          "outputTemplate": "{Timestamp:dd-MMM-yyyy HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
        }
      }
    ]
  }
}

错误

hudson.remoting.ProxyException: net.sf.json.JSONException: 在 net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143) 在 net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103) 的 JSON 字符串无效) 在 net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84) 在 org.jenkinsci.plugins.pipeline.utility.steps.json.ReadJSONStepExecution.doRun(ReadJSONStepExecution.java:77) 在 org.jenkinsci.plugins。 pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:32) at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47) at java.util.concurrent.Executors$RunnableAdapter .call(Executors.java:511) 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent。ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 在 java.lang.Thread.run(Thread.java:748)

标签: jsonjenkins-pipeline

解决方案


除了有关前导/尾随空格的其他答案之外,这也可能是由文件开头的字节顺序标记引起的。

我将文件的编码从带有 BOM 的 UTF-8 更改为 UTF-8,它解决了问题。


推荐阅读