我有一個JAX-RS Rest服務。然後我在Netbeans中調用這個服務。我正在使用genson-1.0。 我所說的服務是這樣的:將Json數組添加到對象列表中
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"}]
錯誤
異常線程 「main」 javax.ws.rs.WebApplicationException:HTTP 500內部服務器錯誤在 com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:127) at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor $ TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:188) 在 org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134) 在 組織.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:988) 在 org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:833) 在 org.glassfish.jersey .message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:768) at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96) 在 org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:740) 在 org.glassfish.jersey.client.JerseyInvocation.access 500 $(JerseyInvocation.java:88) 在 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)在 org.glassfish.jersey.internal.Errors.process(Errors.java:228)在 org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421) 在 org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646) 在 org.glassfish.jersey.client.JerseyInvocation $ Builder.method(JerseyInvocation.java:375) 在 org.glassfish.jersey.client.JerseyInvocation $ Builder.get(JerseyInvocation.java:275) 在 mypackage的.AppointmentRRSClient.allAppointments(AppointmentRRSClient.java:39) 在 mPackage.AppointmentRRSClientTest.main(AppointmentRRSClientTest.java:25) 引起:com.owlike.genson.JsonBindingException:無法 反序列化到在 鍵入類myPackage.Appointment com.owlike.genson.Genson.deserialize(Genson.java:391)at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:125) ... 18更多原因:com.owlike.genson.stream.JsonStreamException: 非法字符在行0和列0期望{但讀'['!在 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) 在 com.owlike.genson.reflect.BeanDescriptor._deserWithCtrArgs(BeanDescriptor.java:120) 在 com.owlike.genson.reflect.BeanDescriptor.deserialize(的BeanDescriptor。Java的:95) 在 com.owlike.genson.convert.BeanViewConverter.deserialize(BeanViewConverter.java:102) 在 com.owlike.genson.convert.NullConverter $ NullConverterWrapper.deserialize(NullConverter.java:56) 在com.owlike.genson.Genson.deserialize(Genson.java:389)... 19多個 Java結果:1 BUILD SUCCESSFUL(總時間:1秒)
是我在一個壞的格式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數據並解析其類型約會的對象的列表。
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。我不知道如何將json轉換爲字符串。有任何想法嗎? – 2014-09-20 14:58:13
我設法解決我使用gson的問題,因爲它接受我的休息服務中的json格式。 – 2014-09-20 16:31:41
@JohnEphraimTugado你的聲明聽起來很奇怪,默認情況下沒有json lib會接受反序列化一個數組到一個對象。有些提供了接受它的選項,但是gson沒有。我沒有看到它如何與Gson(或其他庫)一起工作,而不需要對代碼進行任何更改。 – eugen 2014-09-21 11:18:34