MonoTouch中与Newtonsoft不工作(Monotouch with Newtonsoft

2019-10-16 14:24发布

我是很新的的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 } 

Answer 1:

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

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



文章来源: Monotouch with Newtonsoft not working