2012-04-11 76 views
1

我對Monotouch/iOS開發非常陌生。我正在使用Newtonsoft無法正常工作

========== 
MonoDevelop 2.8.8.4 
Installation UUID: 0d3db625-7df9-4282-9aa6-177c25a46d07 
Runtime: 
     Mono 2.10.9 (tarball Tue Mar 20 15:31:37 EDT 2012) 
     GTK 2.24.10 
     GTK# (2.12.0.0) 
Mono for Android not installed 
Apple Developer Tools: 
     Xcode 4.2.1 (834) 
     Build 4D502 
Monotouch: 5.2.10 (Evaluation) 
========== 

我想使用Newtonsoft 3.5所有序列化對象到json字符串。

非常簡單的代碼,但不工作。可任何人都可以請幫我...

partial void Action_Clicked (MonoTouch.Foundation.NSObject sender) 
{ 
    myDTO test = new myDTO(); 
    string teststring = ObjToJSON(test); 
} 

public string ObjToJSON(myDTO oObject) //Old function name is ObjToJSON_WithWrapper 
{ 
    string sJSON = ""; 
    sJSON = Newtonsoft.Json.JsonConvert.SerializeObject(oObject); 
    return sJSON; 
} 

public myDTO JSONToObj(String JSONString) //Old function name is JSONToObjNew 
{ 
    myDTO deseri = JsonConvert.DeserializeObject<myDTO>(JSONString); 
    return deseri; 
} 

public class myDTO 
{ 
    public myDTO() 
    { 
    } 

    public string StringObject {get; set;} 
} 

以上是我簡單的測試代碼,但是當它運行Newtonsoft.Json.JsonConvert.SerializeObject它給我的錯誤( oObject);

*** ERROR*** 

{System.TypeLoadException: A type load exception has occurred. 
    at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Type,System.Type].AddValue System.Type key) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Type,System.Type].Get (System.Type key) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociatedMetadataType (System.Type type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute] (System.Type type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[JsonContainerAttribute] (ICustomAttributeProvider attributeProvider) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute].AddValue (ICustomAttributeProvider key) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Utilities.ThreadSafeStore`2[System.Reflection.ICustomAttributeProvider,Newtonsoft.Json.JsonContainerAttribute].Get (ICustomAttributeProvider key) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.CachedAttributeGetter`1[Newtonsoft.Json.JsonContainerAttribute].GetAttribute (ICustomAttributeProvider type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonContainerAttribute (System.Type type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonObjectAttribute (System.Type type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe (System.Object value) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, Formatting formatting, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value) [0x00000] in <filename unknown>:0 
    at JSONTest1.JSONTest1ViewController.ObjToJSON (JSONTest1.myDTO oObject) [0x00006] in /Users/developer/Projects/JSONTest1/JSONTest1/JSONTest1ViewController.cs:61 
    at JSONTest1.JSONTest1ViewController.Action_Clicked (MonoTouch.Foundation.NSObject sender) [0x00006] in /Users/developer/Projects/JSONTest1/JSONTest1/JSONTest1ViewController.cs:52 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 
    at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29 
    at JSONTest1.Application.Main (System.String[] args) [0x00000] in /Users/developer/Projects/JSONTest1/JSONTest1/Main.cs:17 } 

回答

3

你需要這個版本Newtonsoft對MonoTouch的:

https://github.com/ayoung/Newtonsoft.Json

+0

感謝快速回復。我下載了這個版本,但它包含解決方案文件,我不知道如何生成.dll文件,我可以在單聲道中使用開發iPhone版本... – Kartik 2012-04-12 15:01:34

+0

非常感謝你..現在工作正常..這是非常多的幫助...再次謝謝你.. – Kartik 2012-04-12 15:33:05