将JSON转换为Java对象时出错

将JSON转换为Java对象时出错,java,rest,Java,Rest,我想将JSON转换为java对象。我创建了一个类,并编写了如下代码。我在位置0处得到异常o/p:意外字符(c) 我正在从客户端获取JSON数据。但是当我尝试将JSON转换为java时,我得到了一个错误 ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean

我想将JSON转换为java对象。我创建了一个类,并编写了如下代码。我在位置0处得到异常
o/p:意外字符(c)

我正在从客户端获取JSON数据。但是当我尝试将JSON转换为java时,我得到了一个错误

        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

        Client client = Client.create(clientConfig);
        WebResource webResource = client
                .resource("my url");
        String name = "adn";
        String password = "12";
        String authString = name + ":" + password;
        String authStringEnc = new BASE64Encoder().encode(authString
                .getBytes());
        // System.out.println("Base64 encoded auth string: " +
        // authStringEnc);
        ClientResponse response = webResource.accept("application/json")
                .header("Authorization", "Basic " + authStringEnc)
                .get(ClientResponse.class);

        String output = response.getEntity(String.class);




         output = output.substring(13, output.length() - 1);

            System.out.println(output);

         JSONParser parse = new JSONParser(); 



counts count=new counts();

         JSONObject jobj = (JSONObject)parse.parse(output); 

         JSONArray jsonarr_1 = (JSONArray) jobj.get("Counts"); 
        // System.out.println(jsonarr_1.get(1));

         JSONArray array = new JSONArray();


        //Get data for Results array

         for(int i=0;i<jsonarr_1.length();i++)

         {

         //Store the JSON objects in an array

         //Get the index of the JSON object and print the values as per the index

         JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);


         System.out.println("Elements under results array");

         System.out.println("\nPlace id: " +jsonobj_1.get("college_name"));

         System.out.println("Types: " +jsonobj_1.get("college_epass_id"));

         }
ClientConfig ClientConfig=newdefaultclientconfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE\u POJO\u映射,Boolean.TRUE);
Client=Client.create(clientConfig);
WebResource=client
.resource(“我的url”);
String name=“adn”;
字符串密码=“12”;
字符串authString=name+“:”+密码;
String authStringEnc=new BASE64Encoder().encode(authString
.getBytes());
//System.out.println(“Base64编码的身份验证字符串:”+
//authStringEnc);
ClientResponse response=webResource.accept(“应用程序/json”)
.header(“授权”、“基本”+authStringEnc)
.get(ClientResponse.class);
字符串输出=response.getEntity(String.class);
output=output.substring(13,output.length()-1);
系统输出打印项次(输出);
JSONParser parse=新的JSONParser();
计数=新计数();
JSONObject jobj=(JSONObject)parse.parse(output);
JSONArray jsonarr_1=(JSONArray)jobj.get(“计数”);
//System.out.println(jsonarr_1.get(1));
JSONArray数组=新的JSONArray();
//获取结果数组的数据

对于(int i=0;i我认为您的问题是截断了这一行的响应

 output = output.substring(13, output.length() - 1);
这将使JSON字符串无效

也许您应该考虑删除这行代码或创建一个单独的变量来存储子字符串。

HR> 杰克逊的使用

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

共享链接以获取更多详细信息。

在我看来,您的JSON没有有效的结构。(我没有查看您的代码){我删除了该行(output=output.substring(13,output.length()-1);),,,,,我遇到另一个异常o/p:java.lang.ClassCastException:org.json.simple.JSONObject无法转换为org.json.JSONObject Hi head,我尝试过这种方法。我遇到另一个异常:o/p:java.lang.ClassCastException:org.json.simple.JSONObject无法转换为org.json.JSONObject看起来您导入了错误的JSONObject类。要吗在类的顶部有一行类似“import org.json.JSONObject”的内容?如果是这样,请将其更改为“import org.json.simple.JSONObject”ya head,我的类中有该导入名称更改导入是否解决了您的问题?hi head,我收到另一个异常o/p:线程“main”中的异常org.codehaus.jackson.map.JsonMappingException:无法反序列化java.util.ArrayList的实例超出START\u对象标记,其显示o/p如下:线程“main”中的异常java.lang.VerifyError:无法从final类继承通常这样的问题是由于不兼容的版本造成的:请确保您的所有Jackson组件都具有相同的次要版本。因此,如果您使用的是Jackson 2.9,则与Jackson相关的所有库都应为2.9版本。您是否可以提供Jackson的JAR任何一个版本。我找不到用于相同的版本。根据需要的版本从下面的共享位置下载。我下载了jars rohan。我收到另一个错误o/p:线程“main”org.codehaus.jackson.map.JsonMappingException中的异常:无法反序列化java.util.ArrayList的实例,超出START_对象标记
public class CollegeList{
  @JsonProperty("counts")
  public List<College> counts;
}



 public class College{
     @JsonProperty("college_name") public String college_name;
     @JsonProperty("college_epass_id") public int college_epass_id;
    }
CollegeList colleges = mapper.readValue(jsonString, CollegeList .class);