我试图在项目中使用wcf服务对json数组中的数据进行DeSerialize。 数据被提取并且可以在调试中被注意到,但是我无法反序列化,或者我错误的其他内容。以下是我尝试反序列化的代码。 注意: - 我已经以正确的格式单独运行wcf应用程序及其返回的json数组与实际结果。
HotelServiceClient.cs
public class HotelServiceClient
{
private string BASE_URL = "http://localhost:50540/ServiceHotel.svc/";
public List<HotelInfo> findall()
{
try
{
var webClient = new WebClient();
var json = webClient.DownloadString(BASE_URL + "findall");
var javaScriptJson = new JavaScriptSerializer();
return javaScriptJson.Deserialize<List<HotelInfo>>(json);
}
catch
{
return null;
}
}
HotelInfo.cs
public class HotelInfo
{
private int _hotelid;
private string _hotelname;
private string _hoteldesc;
private string _hotelprice;
private byte[] _hotelpicture;
[Key]
[Display(Name = "Id")]
public int Hotelid
{
get
{
return _hotelid;
}
set
{
_hotelid = value;
}
}
[Display(Name = "Name")]
public string Hotelname
{
get
{
return _hotelname;
}
set
{
_hotelname = value;
}
}
[Display(Name = "description")]
public string Hoteldesc
{
get
{
return _hoteldesc;
}
set
{
_hoteldesc = value;
}
}
[Display(Name = "price")]
public string Hotelprice
{
get
{
return _hotelprice;
}
set
{
_hotelprice = value;
}
}
[Display(Name = "picture")]
public byte[] Hotelpicture
{
get
{
return _hotelpicture;
}
set
{
_hotelpicture = value;
}
}
}
的WebService
Hotel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CRUDwithHotels
{
public class Hotel
{
public int Id { get; set; }
public string Name { get; set; }
public string description { get; set; }
public string price { get; set; }
public byte[] picture { get; set; }
}
}
IServiceHotel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace CRUDwithHotels
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IServiceHotel" in both code and config file together.
[ServiceContract]
public interface IServiceHotel
{
[OperationContract]
[WebInvoke(Method ="GET", UriTemplate ="findall", ResponseFormat =WebMessageFormat.Json)]
List<Hotel> findall();
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "find/{id}", ResponseFormat = WebMessageFormat.Json)]
Hotel find(string id);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "create", ResponseFormat = WebMessageFormat.Json, RequestFormat =WebMessageFormat.Json)]
bool create(Hotel hotel);
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "edit", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
bool edit(Hotel hotel);
[OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "delete", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
bool delete(Hotel hotel);
}
}
ServiceHotel.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace CRUDwithHotels
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ServiceHotel" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select ServiceHotel.svc or ServiceHotel.svc.cs at the Solution Explorer and start debugging.
public class ServiceHotel : IServiceHotel
{
public bool create(Hotel hotel)
{
using (ModelMyDemo hie = new ModelMyDemo())
{
try
{
HotelInfoEntities info = new HotelInfoEntities();
info.Hotelname = hotel.Name;
info.Hoteldesc = hotel.description;
info.Hotelprice = hotel.price;
info.Hotelpicture = hotel.picture;
hie.HotelInfoEntities.Add(info);
hie.SaveChanges();
return true;
}
catch
{
return false;
}
};
}
public bool delete(Hotel hotel)
{
using (ModelMyDemo hie = new ModelMyDemo())
{
try
{
int id = Convert.ToInt16(hotel.Id);
HotelInfoEntities info = hie.HotelInfoEntities.Single(p => p.Hotelid == id);
hie.HotelInfoEntities.Remove(info);
hie.SaveChanges();
return true;
}
catch
{
return false;
}
};
}
public bool edit(Hotel hotel)
{
using (ModelMyDemo hie = new ModelMyDemo())
{
try
{
int id = Convert.ToInt16(hotel.Id);
HotelInfoEntities info = hie.HotelInfoEntities.Single(p => p.Hotelid == id);
info.Hotelname = hotel.Name;
info.Hoteldesc = hotel.description;
info.Hotelprice = hotel.price;
info.Hotelpicture = hotel.picture;
hie.SaveChanges();
return true;
}
catch
{
return false;
}
};
}
public Hotel find(string id)
{
int hid = Convert.ToInt16(id);
using (ModelMyDemo hie = new ModelMyDemo())
{
return hie.HotelInfoEntities.Where(pe => pe.Hotelid == hid).Select(pe => new Hotel
{
Id = pe.Hotelid,
Name = pe.Hotelname,
description = pe.Hoteldesc,
price = pe.Hotelprice,
picture = pe.Hotelpicture
}).First();
};
}
public List<Hotel> findall()
{
//var imagesrc = string.Format("data:image/jpeg;base64,{0}", base64);
using (ModelMyDemo hie = new ModelMyDemo())
{
return hie.HotelInfoEntities.Select(pe => new Hotel
{
Id = pe.Hotelid,
Name = pe.Hotelname,
description = pe.Hoteldesc,
price = pe.Hotelprice,
picture = pe.Hotelpicture
// picture = pe.Hotelpicture
}).ToList();
};
}
}
}