我有 JAX-RS Rest 服务。然后我在 Netbeans 中调用此服务。我正在使用 genson-1.0。 我这样调用该服务:
ArrayList<Appointment> appointments;
AppointmentRRSClient client = new AppointmentRRSClient();
Object response = client.allAppointments(Appointment.class);
我的 JSON 数组
[{"id":1,"date":"2014-09-19","patient_id":1,"patient_name":"Lorenzana, Jerome Keith G.","patient_mobile":"+639178374407","staff_id":1,"staff_name":"Tugado, John Ephraim G.","location":"Makati Health Center","type":"walk-in","remarks":"okay lang"},{"id":2,"date":"2014-09-19","patient_id":2,"patient_name":"Black, Gucci G.","patient_mobile":"+639178488120","staff_id":1,"staff_name":"Tugado, John Ephraim G.","location":"Makati Health Center","type":"walk-in","remarks":"okay lang"},{"id":3,"date":"2014-09-19","patient_id":3,"patient_name":"Sagucio, Matthew V.","patient_mobile":"+6391068753242","staff_id":1,"staff_name":"Tugado, John Ephraim G.","location":"Makati Health Center","type":"walk-in","remarks":"okay lang"},{"id":4,"date":"2014-09-19","patient_id":4,"patient_name":"Lizardo, Daniel Z.","patient_mobile":"+639175606349","staff_id":1,"staff_name":"Tugado, John Ephraim G.","location":"Makati Health Center","type":"walk-in","remarks":"okay lang"},{"id":5,"date":"2014-09-19","patient_id":5,"patient_name":"Abulencia, Chester X.","patient_mobile":"+639051200480","staff_id":1,"staff_name":"Tugado, John Ephraim G.","location":"Makati Health Center","type":"walk-in","remarks":"okay lang"}]
错误
Exception in thread "main" javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:127) at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:188) at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134) at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:988) at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:833) at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:768) at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96) at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:740) at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88) at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:228) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:375) at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:275) at myPackage.AppointmentRRSClient.allAppointments(AppointmentRRSClient.java:39) at mPackage.AppointmentRRSClientTest.main(AppointmentRRSClientTest.java:25) Caused by: com.owlike.genson.JsonBindingException: Could not deserialize to type class myPackage.Appointment at com.owlike.genson.Genson.deserialize(Genson.java:391) at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:125) ... 18 more Caused by: com.owlike.genson.stream.JsonStreamException: Illegal character at row 0 and column 0 expected { but read '[' ! at com.owlike.genson.stream.JsonReader.newWrongTokenException(JsonReader.java:949) at com.owlike.genson.stream.JsonReader.begin(JsonReader.java:425) at com.owlike.genson.stream.JsonReader.beginObject(JsonReader.java:157) at com.owlike.genson.reflect.BeanDescriptor._deserWithCtrArgs(BeanDescriptor.java:120) at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:95) at com.owlike.genson.convert.BeanViewConverter.deserialize(BeanViewConverter.java:102) at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56) at com.owlike.genson.Genson.deserialize(Genson.java:389) ... 19 more Java Result: 1 BUILD SUCCESSFUL (total time: 1 second)
我的 JSON 格式是否错误?
Illegal character at row 0 and column 0 expected { but read '['
当我获取单个数据时,一切正常。
{"username":null,"password":null,"staff_id":1,"staff_name":"Tugado, John Ephraim G.","staff_gender":"male","staff_position_id":1,"health_center_id":1,"health_center_name":"Makati Health Center","account_type":"administrator","account_id":1,"staff_position":"Doctor"}
提前致谢!
编辑:
AppointmentRRSClient 有此方法来获取 json 数据并将其解析为 Appointments 类型的对象列表。
public <T> T allAppointments(Class<T> responseType) throws ClientErrorException {
WebTarget resource = webTarget;
resource = resource.path("allAppointments");
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
解决方案
我用了Gson 。现在我这样调用该服务。
List<String> names = new ArrayList<String>();
URL url = new URL("http://my-url.com/rest/name");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String response = "";
String output;
while ((output = br.readLine()) != null) {
response += output;
}
conn.disconnect();
// System.out.println(response);
Gson gson = new Gson();
TypeToken<List<String>> token = new TypeToken<List<String>>(){};
names = gson.fromJson(response, token.getType());
最佳答案
您的 json 包含一个约会数组,但您正在反序列化为约会对象。使用子字符串的解决方案可能适用于这种情况,但它既不干净也不稳定(如果你想反序列化具有多个元素的列表会发生什么 - 你得到第一个......),你不应该这样做这边走。
干净的解决方案是将方法签名更改为:
public <T> T allAppointments(Class<T> responseType)
至
public <T> T allAppointments(GenericType<T> responseType) {
WebTarget resource = webTarget;
resource = resource.path("allAppointments");
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
然后你这样调用它:
AppointmentRRSClient client = new AppointmentRRSClient();
List<Appointment> response = client.allAppointments(new GenericType<List<Appointment>>(){});
备注 GenericType 不是来自 Genson 的,而是来自 Jersey 的:javax.ws.rs.core.GenericType
关于java - Json 数组到对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25948702/