我有一个用 ASP.NET 编写的 Web 服务,它接受 Contact( List ) 类型的列表。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
#region List of contacts that need to be created in the database.
public List<KeyValue> createRecordsInDb(List<Contact> dataToPass)
{
return Contact.insertArrayIntoDb(dataToPass);
}
#endregion
在 android 设备上,我使用 GSON 库将List转换为String 。
List<Contact> lstContactsToCreate= retrieveDataFromCursor(cursor,false);
String jsonString = new Gson().toJson(lstContactsToCreate);
try {
callWebService(jsonString, _context.getResources().getString(R.string.updateCreateURL), false);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
当我希望将数据发送到 Web 服务时,就会出现问题。我通过以下方法传递数据,
private HttpResponse callWebService(String dataToPass, String URL, boolean isUpdate)
throws JSONException, ClientProtocolException, IOException{
HttpPost httpPost = new HttpPost(URL);
httpPost.setHeader("content-type", "application/json");
HttpClient httpClient = new DefaultHttpClient(getHttpParameterObj(4000,4000));
// Building the JSON object.
JSONObject data = new JSONObject();
data.put("dataToPass", dataToPass);
StringEntity entity = new StringEntity(data.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
entity.setContentEncoding( "UTF-8");
httpPost.setEntity(entity);
// Making the call.
HttpResponse response = httpClient.execute(httpPost);
return response;
}
每当我调用网络服务时,它都会引发以下错误 -
{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1
问题是data.toString()
返回:-
{"dataToPass":"[{\"address\":\"Himayath Nagar\",\"email\":\"abijeet.p@osmosys.asia\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"saravana.e@osmosys.asia\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"asshole@gmail.com\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]"}
而网络服务需要格式的数据
{"dataToPass":[{\"address\":\"Himayath Nagar\",\"email\":\"abijeet.p@osmosys.asia\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"saravana.e@osmosys.asia\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"asshole@gmail.com\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]}
我什至尝试替换和删除那些“”:-
data.toString().replace("\"[","[").replace("]\"","]");
(java.lang.String) {"dataToPass":[{\"address\":\"Himayath Nagar\",\"email\":\"abijeet.p@osmosys.asia\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"saravana.e@osmosys.asia\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"asshole@gmail.com\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]}
但这似乎不起作用,因为 Web 服务器会引发错误
{"Message":"Invalid object passed in, member name expected. (16): {\"dataToPass\":[{\\\"address\\\":\\\"Himayath Nagar\\\",\\\"email\\\":\\\"abijeet.p@osmosys.asia\\\",\\\"name\\\":\\\"Abijeet Patro\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"},{\\\"address\\\":\\\"himayath naga\\\",\\\"email\\\":\\\"saravana.e@osmosys.asia\\\",\\\"name\\\":\\\"Saravana\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"},{\\\"address\\\":\\\"hellpo world\\\",\\\"email\\\":\\\"asshole@gmail.com\\\",\\\"name\\\":\\\"Syai\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"}]}","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
那么如何将一组用户定义的对象从 Java 传递到我的 ASP.NET Web 服务?或者有没有办法可以将 asp.net Web 服务器上的 GSON 字符串转换为我的 List 对象?
我可以使用 JSON.NET 将字符串反序列化到我的列表吗?