gpt4 book ai didi

java - 当我尝试解析 JSON 字符串时收到意外的内部错误

转载 作者:行者123 更新时间:2023-12-02 04:03:02 25 4
gpt4 key购买 nike

我正在尝试解析来 self 的 Python Web 服务器的其余响应。我成功获取了JSON内容的字符串。为了简化问题,我现在只是尝试解析如下所示的测试字符串。

我的 python 测试用例可以很好地解析字符串,但这是针对 java 插件的,所以我尝试将其转换为 java。

我遵循了一些看似相同示例的副本。

               String inputJSONs = "{\"connections\":[{\"num\":\"1\"},{\"num\":\"2\"}]}";


System.out.println("testing");
JSONObject jsono = new JSONObject(inputJSONs);

System.out.println("parsed json");
JSONArray arr = jsono.getJSONArray("connections");

System.out.println("have arr");
System.out.println(arr);


错误 o.a.g.rest.RESTExceptionMapper - 意外的内部错误:org/json/JSONObject

我认为插件框架(apache guacamole)可能隐藏了一些更详细的错误,但我不能确定。

添加完整的示例(删除尽可能多的其他内容以重现)

package org.apache.guacamole.auth;

import java.util.HashMap;
import java.util.Map;

import java.lang.StringBuilder;
import org.json.JSONObject;
import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException;
import org.apache.guacamole.environment.Environment;
import org.apache.guacamole.environment.LocalEnvironment;
import org.apache.guacamole.net.auth.simple.SimpleAuthenticationProvider;
import org.apache.guacamole.net.auth.Credentials;
import org.apache.guacamole.protocol.GuacamoleConfiguration;

public class RestAuthenticationProvider extends SimpleAuthenticationProvider {

private final Environment environment;

private Logger logger = LoggerFactory.getLogger(RestAuthenticationProvider.class.getClass());

public RestAuthenticationProvider() throws GuacamoleException{
environment = new LocalEnvironment();
}

@Override
public String getIdentifier() {
logger.info("====Get Identifier====");
return "Rest API";
}

@Override
public Map<String, GuacamoleConfiguration>
getAuthorizedConfigurations(Credentials credentials)
throws GuacamoleException {

if (credentials.getUsername() == null ){
return null;
}

String inputJSONs = "{\"connections\":[{\"num\":\"1\"},{\"num\":\"2\"}]}";
System.out.println("testing 2");
System.out.println(inputJSONs);
JSONObject jsono = new JSONObject(inputJSONs);
System.out.println("parsed json");
JSONArray arr = jsono.getJSONArray("connections");
System.out.println("have arr");
System.out.println(arr);

return null;
}

}

登录服务器后立即输出以下内容

testing 2
{"connections":[{"num":"1"},{"num":"2"}]}
12:00:00.129 [http-nio-8080-exec-8] ERROR o.a.g.rest.RESTExceptionMapper - Unexpected internal error: org/json/JSONObject

最佳答案

在鳄梨酱特定论坛的帮助下,我能够缩小范围。最终的修复是让 Maven 构建一个包含内置依赖项的 fat jar。

我将以下内容添加到我的 pom.xml

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.mkyong.core.utils.App</mainClass>
</manifest>
</archive>

</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

关于java - 当我尝试解析 JSON 字符串时收到意外的内部错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56724864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com