我已经解决了问题,
我认为我的问题是我不知道如何在 webservice .NET 中获取 JSON
我使用 android studio 将 JSON 对象解析到我的 web 服务,JSON 数组和 JSON 对象正在工作,
但是我收到了一个错误的请求(也许是因为我无法在网络服务中获取 JSON?)
JSONObject jsonObject = new JSONObject();
jsonObject.put("horary", jsonArray);
URL url = new URL("URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(4000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept","application/json");
conn.setDoOutput(true);
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
os.flush();
os.close();
Log.d("json", conn.getResponseMessage());<- bad Request
这是我来自 webservice vb.net 的代码。我不知道如何获取 JSON 对象
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="Change/")>
Function HReceiver(horary As String) As String Implements IService.HReceiver
我想我用 (horary As String) 得到了 JSON,并且我尝试了很多方法来反序列化,
但这不是重点,因为我想我无法获取 JSON
Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
Dim horary As ArrayHorary
Dim result As String
Try
Dim items = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ArrayHorary())(horary)
result = horary.ToString
Catch ex As Exception
result = ex.ToString
End Try
Return result
End Function
Public Class ArrayHorary
Public Property id As String
Public Property name As String
End Class
谢谢您的宝贵时间
------------我解决了
<OperationContract()>
Function HReceiver(horary As Horar) As String
<DataContract()>
Public Class Horar
<DataMember()>
Public Property horary As List(Of Horari)
End Class
<DataContract()>
Public Class Horari
<DataMember()>
Public Property id As String
<DataMember()>
Public Property name As String
End Class
服务中
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="Change/")>
Function HReceiver(horar As Horar) As String Implements IService.HReceiver
Dim result As String
result = horar.horary(0).name
return result
并且无需解串器即可工作
在android中,我必须使用
os.writeChars(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
最佳答案
要以正确的方式接收 JSON 对象,您必须拥有一个与函数参数具有相同结构的类,例如,如果您正在接收此对象的列表,则 api 参数应该是:
Function SpoolsList(objVar As List(Of ArrayHorary)) As IHttpActionResult
Your code
End Function
然后它会自动解析该对象。
关于java - 从android解析jsonObject并在webservice vb.net中获取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525695/