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

时间:2019-06-23 14:30:13

标签: java json rest

我正在尝试从我的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

1 个答案:

答案 0 :(得分:0)

在鳄梨酱特定论坛的帮助下,我可以缩小范围。最终的解决方法是让Maven使用内置的依赖项构建一个胖子。

我在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>