我将首先展示我想要反序列化的JSON:
{"FleetsCollection":[{"FleetId":2,"Nickname":"2007 Ninja ZX6R","PictureFileName":"jvmlfdaq.rkr2.jpg","AverageMpg":43.90925,"MaxMpg":47.945},{"FleetId":44,"Nickname":"Luminous Neptune","PictureFileName":"ochufm0c.ohm2.png","AverageMpg":29.4285,"MaxMpg":30.341}]}
这来自Fleets对象,其中包含Fleet对象的列表集合,如下所示:
public class Fleets
{
private List<Fleet> fleets = new List<Fleet>();
}
自定义Fleet对象编写如下:
public class Fleet
{
public int FleetId { get; set; }
public string Nickname { get; set; }
public string PictureFileName { get; set; }
public double AverageMpg { get; set; }
public double MaxMpg { get; set; }
}
最后,我的反序列化代码显示在这里。我相信这里有问题的部分是ConvertObject方法中的几行:
public class DataAccessState<T>
{
public string Uri { get; set; }
public T CallingClassType { get; set; }
public string MethodToCall { get; set; }
public DataAccessState(){}
public DataAccessState(string uri, T callingClassType, string methodToCall)
{
Uri = uri;
CallingClassType = callingClassType;
MethodToCall = methodToCall;
}
}
public static class DataAccessList<T>
{
private static List<DataAccessState<T>> dataAccessStates = new List<DataAccessState<T>>();
public static void Add(DataAccessState<T> dataAccessState)
{
dataAccessStates.Add(dataAccessState);
}
public static DataAccessState<T> FindAndRemove(string uri)
{
var dataAccessState = new DataAccessState<T>();
foreach (var das in dataAccessStates)
{
if (das.Uri == uri)
dataAccessState = das;
}
dataAccessStates.Remove(dataAccessState);
return dataAccessState;
}
}
public class RequestUpdateState
{
public HttpWebRequest AsyncRequest { get; set; }
public HttpWebResponse AsyncResponse { get; set; }
}
public class DataAccess<T>
{
public void GetObject(string uriQuery, T callingClassType, string methodToCall)
{
//Create full uri
var fullUri = "http://fuelizer.com/MobileJSON.svc/" + uriQuery;
//Add calling type to list
DataAccessState<T> dataAccessState = new DataAccessState<T>(fullUri, callingClassType, methodToCall);
DataAccessList<T>.Add(dataAccessState);
//Perform web service call
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(fullUri));
RequestUpdateState requestState = new RequestUpdateState {AsyncRequest = request};
request.BeginGetResponse(GetDataResponse, requestState);
}
private void GetDataResponse(IAsyncResult asyncResult)
{
try
{
RequestUpdateState requestState = (RequestUpdateState)asyncResult.AsyncState;
HttpWebRequest request = requestState.AsyncRequest;
requestState.AsyncResponse = (HttpWebResponse)request.EndGetResponse(asyncResult);
Stream jsonObject = requestState.AsyncResponse.GetResponseStream();
Deployment.Current.Dispatcher.BeginInvoke(() => ConvertObject(requestState.AsyncRequest.RequestUri.AbsoluteUri, jsonObject));
}
catch (WebException e){}
}
private void ConvertObject(string uri, Stream jsonObject)
{
var dataAccessState = DataAccessList<T>.FindAndRemove(uri);
DataContractJsonSerializer ser = new DataContractJsonSerializer(dataAccessState.CallingClassType.GetType());
var returnedObject = (T)ser.ReadObject(jsonObject);
MethodInfo methodInfo = returnedObject.GetType().GetMethod(dataAccessState.MethodToCall);
methodInfo.Invoke(returnedObject, null);
}
}
我会得到一个带有空列表集合的返回Fleets对象。此相同的代码适用于没有集合的对象。因此,如果我只是返回一个Fleet对象,我会毫无疑问得到我的回复。看来这个反序列化器不能反序列化包含自定义类型列表集合的对象???
答案 0 :(得分:3)
您的'车队'财产需要公开,名称需要匹配。试试吧:
public class Fleets
{
public List<Fleet> FleetsCollection = new List<Fleet>();
}
public class Fleet
{
public int FleetId { get; set; }
public string Nickname { get; set; }
public string PictureFileName { get; set; }
public double AverageMpg { get; set; }
public double MaxMpg { get; set; }
}
答案 1 :(得分:0)
使用json.net库,因为DataContractJsonSerializer不支持字典的反序列化。
你可以试试像
这样的东西var result = JsonConvert.DeserializeObject(yourclass)(你的字符串); (为你的班级设置角括号)