【问题标题】:JSON not working on iOS app Unity 5JSON不适用于iOS应用Unity 5
【发布时间】:2016-05-24 04:57:54
【问题描述】:

我正在 Unity 上开发 iOS 应用程序,但遇到了问题。 对于数据存储,我使用 JSON,在 Unity 中我使用 Newtonsoft.Json(我从官方网站下载 dll 文件并添加到 .Net 2.0 的 Unity 项目版本)。

当我在 Mac 上的 Unity 中启动场景时一切正常,但是当我为 iPhone 构建项目并在 iPhone XCode 上启动场景时输出此日志(并且元素不会添加到下拉菜单)。

在来自 json 文件的场景中,我得到了我的元素列表并添加到下拉列表中。

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);
}

日志:

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)

【问题讨论】:

标签: xcode unity3d json.net


【解决方案1】:

使用 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);
    }
}

实用提示

当您遇到此类问题时,还可以考虑使用此site 从 Json 生成类。完成后,只需将所有Lists 更改为arrays 并从所有变量中删除自动属性{ get; set; }。 Unity 内置的 Json 序列化器将工作。使用 5.40.B13 测试,如果您有任何问题,我建议您更新到此版本。

【讨论】:

  • 谢谢!它工作正常。我不会将 List 更改为数组。
  • @IvanIstomin 欢迎您。当我说将 List 更改为 Array 时,我的意思是当您使用我在答案中为您的 Json 生成类的网站时应该这样做。现在你不必这样做,因为我已经为你做了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多