Monotouch与Newtonsoft无法正常工作

时间:2012-04-11 19:22:14

标签: c# xamarin.ios

我是Monotouch / iOS开发的新手。我正在使用

========== 
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 } 

1 个答案:

答案 0 :(得分:3)

你需要这个版本的Newtonsoft for MonoTouch:

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