我试图反序列化ParseObject(如parse platform developer's guide中所述)。我收到JSON无法反序列化该对象的错误。
所以我创造了这个:
public class Phase
{
public string Details { get; set; }
public string ObjectId { get; set; }
public int PhaseId { get; set; }
public bool Completed { get; set; }
public string PhaseListName { get; set; }
public int Position { get; set; }
public int ButtonId { get; set; }
}
private List<ParseObject> ConvertToParseObjectList(List<Phase> phaseObjects)
{
var _objects = new List<ParseObject>();
for (int i = 0; i < phaseObjects.Count; i++)
{
var _obj = new ParseObject("CasePhases");
_obj.ObjectId = phaseObjects[i].ObjectId;
_obj["Details"] = phaseObjects[i].Details;
_obj["PhaseId"] = phaseObjects[i].PhaseId;
_obj["Completed"] = phaseObjects[i].Completed;
_objects.Add(_obj);
}
return _objects;
}
private List<Phase> ConvertToPhaseObjectList(List<ParseObject> parseObjects)
{
var target = parseObjects.ConvertAll(x => new Phase
{
Details = x.Get<string>("Details"),
ObjectId = x.ObjectId,
PhaseId = x.Get<int>("PhaseId"),
Completed = x.Get<bool>("Completed")
});
return target;
}
所以我可以这样序列化和反序列化:
ConvertToParseObjectList(JsonConvert.DeserializeObject<List<Phase>>(Settings.PhaseObjects));
JsonConvert.SerializeObject(ConvertToPhaseObjectList( application.casePhases));
它有效,但我试图找出如何告诉JSON如何使用JsonObject和JsonProperty或类似的东西反序列化对象。
引发异常的一行是:
JsonConvert.DeserializeObject<List<ParseObject>>(Settings.PhaseObjects);
跟踪是:
Xamarin caused by: android.runtime.JavaProxyThrowable: Newtonsoft.Json.JsonSerializationException: Cannot create and populate list type Parse.ParseObject. Path '[0]', line 1, position 2.
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList(JsonReader reader, JsonArrayContract contract, ref bool createdFromNonDefaultCreator)<2781d1b198634655944cdefb18b3309b>:0
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string id)<2781d1b198634655944cdefb18b3309b>:0
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)<2781d1b198634655944cdefb18b3309b>:0
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, string id)<2781d1b198634655944cdefb18b3309b>:0
md5b8608fa4770a86d5bda0b33ebcfbec27.LiveSurgery.n_onResume(Native Method)
md5b8608fa4770a86d5bda0b33ebcfbec27.LiveSurgery.onResume()LiveSurgery.java:65
android.app.Instrumentation.callActivityOnResume()Instrumentation.java:1197
android.app.Activity.performResume()Activity.java:5496
android.app.ActivityThread.performResumeActivity()ActivityThread.java:2975
android.app.ActivityThread.handleResumeActivity()ActivityThread.java:3018
android.app.ActivityThread.handleLaunchActivity()ActivityThread.java:2422
android.app.ActivityThread.access$800()ActivityThread.java:151
android.app.ActivityThread$H.handleMessage()ActivityThread.java:1342
android.os.Handler.dispatchMessage()Handler.java:110
android.os.Looper.loop()Looper.java:193
android.app.ActivityThread.main()ActivityThread.java:5322
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke()Method.java:515
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:829
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:645
dalvik.system.NativeStart.main(Native Method)