转换从Resful Web Service返回的JSON响应
本文关键字:JSON 响应 返回 Service Resful Web 转换 | 更新日期: 2023-09-27 18:26:03
我的RESTFUL web服务和web应用程序中有这两个类
public class Service
{
public int ID { get; set; }
public int HCode { get; set; }
public int HUserID { get; set; }
public string PatientNID { get; set; }
public List<Document> DocumentList { get; set; }
public int ServiceTypeID { get; set; }
public string TestName { get; set; }
public string Comment { get; set; }
public DateTime Date { get; set; }
}
public class Document
{
public int ID { get; set; }
public int ServiceID { get; set; }
public string FileName { get; set; }
public string Extension { get; set; }
public byte[] ByteDocument { get; set; }
}
这是web服务中接收对象和发回列表的功能
public List<Serivce> GetServiceByHUserID(Service service)
{
SqlCommand cmd = new SqlCommand(
"SELECT *"
+ " FROM TransactionHistory"
+ " WHERE UserID = @HUserUserIDParam"
, con);
cmd.Parameters.Add(
"@HUserUserIDParam", SqlDbType.NVarChar).Value = service.HUserID;
return GetServiceResponse(cmd);
}
private List<Service> GetServiceResponse(SqlCommand cmd)
{
List<Service> serviceList = new List<Service>();
try
{
// Filling Data
....
}
return serviceList;
}
catch (Exception e) { return null; }
finally { con.Close(); }
}
这是web应用中的功能发送对象和接收列表
private List<Service> GetServiceListResponse(HttpWebRequest request
, Service service)
{
try
{
var json = new JavaScriptSerializer().Serialize(service);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = json.Length;
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(),
System.Text.Encoding.ASCII);
requestWriter.Write(json);
requestWriter.Close();
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
JavaScriptSerializer serializer = new JavaScriptSerializer();
// ERROR HERE
dynamic item = serializer.Deserialize<List<Service>>(response);
// ERROR HERE
responseReader.Close();
return item;
}
catch (Exception e) { return null; }
}
当我尝试返回List<时;。Service>作为JSON对象并对其进行反序列化,我得到了错误
这是的例外
无法将"System.String"类型的对象转换为"System.Byte[]"类型的
我发现了解决方案
它只是更改了我在序列化和反序列化中使用的类(.NET中的内置类,名为JavaScriptSerializer)
从JavaScriptSerializer()到JsonConvert
Github上的发布:https://github.com/JamesNK/Newtonsoft.Json/releases
NuGut上的最新软件包:安装软件包Newtonsoft.Json
Newtonsoft网站:http://www.newtonsoft.com/json
在引用dll或下载包之后,引用代码中的dll(在.cs类中):
using Newtonsoft.Json;
其他参考文献:
using System.IO;
using System.Text;
在发送和使用UTF8编码之前序列化json对象:
string json = JsonConvert.SerializeObject(service);
request.Method = "POST";
request.Method = "POSt";
request.ContentType = "application/json";
request.ContentLength = Encoding.UTF8.GetByteCount(json);
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), new UTF8Encoding(false));
requestWriter.Write(json);
requestWriter.Close();
返回响应后反序列化json对象:
List<Service> serviceList = JsonConvert.DeserializeObject<List<Service>>(response);