Reputation: 1408
I will start off by showing the JSON I would like to deserialize:
{"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}]}
This comes from a Fleets object which contains a list collection of Fleet objects like so:
public class Fleets
{
private List<Fleet> fleets = new List<Fleet>();
}
The custom Fleet object is written as below:
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; }
}
Finally, my deserialization code is shown here. I believe the questionable part here would be the few lines in the ConvertObject method:
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);
}
}
What happens is I get a returned Fleets object with an empty list collection. This same code works with objects that do not have collections. So if I were just returning a Fleet object I would have no problem getting my response. It seems that this deserializer is not capable of deserializing an object which contains a list collection of custom types???
Upvotes: 1
Views: 361
Reputation: 371
Your 'fleets' property needs to be public and the names need to match up. Try it with this:
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; }
}
Upvotes: 3
Reputation: 365
use json.net library as DataContractJsonSerializer does not support de-serialization of Dictionary.
you can try something like
var result = JsonConvert.DeserializeObject(yourclass)(your string); (put angular bracket for your class)
Upvotes: 0