2016-05-23 8 views
0

Ich arbeite an iOS-App auf Unity und ich bekomme ein Problem. Für die Datenspeicherung verwende ich JSON, und in Unity verwende ich Newtonsoft.Json (Ich lade dll-Dateien von der offiziellen Website herunter und füge die Unity-Projektversion für .Net 2.0 hinzu).JSON funktioniert nicht auf iOS-App Einheit 5

Wenn ich Szene in Unity auf Mac starte alles funktioniert gut, aber wenn ich Projekt für iPhone erstellen und Szene auf dem iPhone starten XCode Ausgabe dieses Protokoll (und Elemente nicht zu Dropdown hinzufügen).

In Szene von JSON-Datei bekomme ich Liste meiner Elemente und DropDown hinzufügen.

public Dropdown anions; 
public Dropdown cations; 

StreamReader reader; 
string json; 
SolubilityTable solubility; 

public void Start() { //When I start Scene 
    reader = new StreamReader (Application.dataPath + "/Data/solubilityTable.json"); 
    json = reader.ReadToEnd(); 
    solubility = JsonConvert.DeserializeObject<SolubilityTable>(json); 

    anions.AddOptions (solubility.anions); 
    cations.AddOptions (solubility.cations); 
} 

Log:

MissingMethodException: Method not found: 'Default constructor not  found...ctor() of System.ComponentModel.TypeConverter'. 
    at System.Activator.CreateInstance (System.Type type, Boolean  nonPublic) [0x00000] in <filename unknown>:0 
    at System.Activator.CreateInstance (System.Type type) [0x00000] in  <filename unknown>:0 
    at System.ComponentModel.TypeDescriptor.GetConverter (System.Type  type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonTypeReflector.GetTypeConverter  (System.Type type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Utilities.ConvertUtils.GetConverter (System.Type  t) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.DefaultContractResolver.CanConvertToString  (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.JsonSerializerInternalReader.GetContractSafe (System.Type type) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0 
    at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <filename unknown>:0 
    at Solubility.Start() [0x00000] in <filename unknown>:0 

(Filename: currently not available on il2cpp Line: -1) 
+0

Wie sieht der JSon aus, den Sie erhalten? Können Sie die hier empfangene JSON-Datei einfügen? – Programmer

+0

@Programmierer https://github.com/diniska/chemistry/blob/master/PeriodicalTable/periodicTable.json –

Antwort

0

Verwenden Unity Jason API JsonUtility.FromJson.

[System.Serializable] 
public class Element 
{ 
    public string group; 
    public int position; 
    public string name; 
    public object number; 
    public string small; 
    public double molar; 
    public int[] electrons; 
} 

[System.Serializable] 
public class Table 
{ 
    public string wiki; 
    public Element[] elements; 
} 

[System.Serializable] 
public class Lanthanoid 
{ 
    public string group; 
    public int position; 
    public string name; 
    public int number; 
    public string small; 
    public double molar; 
    public int[] electrons; 
} 

[System.Serializable] 
public class Actinoid 
{ 
    public string group; 
    public int position; 
    public string name; 
    public int number; 
    public string small; 
    public double molar; 
    public int[] electrons; 
} 

[System.Serializable] 
public class SolubilityTable 
{ 
    public Table[] table; 
    public Lanthanoid[] lanthanoids; 
    public Actinoid[] actinoids; 
} 

System.IO.StreamReader reader; 
string json; 
SolubilityTable solubility; 

void Start() 
{ 
    solubility = new SolubilityTable(); 
    reader = new System.IO.StreamReader(Application.dataPath + "/Data/solubilityTable.json"); 
    json = reader.ReadToEnd(); 
    solubility = JsonUtility.FromJson<SolubilityTable>(json); 


    //Show all wiki from Table 
    for (int i = 0; i < solubility.table.Length; i++) 
    { 
     Debug.Log("Got: " + solubility.table[i].wiki); 
    } 
} 

Hilfreiche Hinweis:

Sehen Sie sich auch diese site mit Klasse von Json zu erzeugen, wenn Sie Probleme wie diese haben. Wenn Sie fertig sind, ändern Sie einfach alle Lists zu arrays und entfernen Sie die automatische Eigenschaft { get; set; } von allen Variablen. Der eingebaute Json-Serializer von Unity wird dann funktionieren. Getestet mit 5.40.B13 und ich empfehle Ihnen, auf diese Version zu aktualisieren, wenn Sie irgendein Problem haben.

+0

Vielen Dank! Es funktioniert gut. Ich ändere List nicht in Array. –

+0

@IvanIstomin Sie sind willkommen. Als ich sagte, ändere List zu Array, meinte ich, du solltest das tun, wenn du die Webseite verwendest, die ich in die Antwort geschrieben habe, um eine Klasse für deinen Json zu generieren. Für jetzt müssen Sie nicht, weil ich das bereits für Sie getan habe. – Programmer

Verwandte Themen