将嵌套的json解析为统一

我有一个解析这个json的问题:

{ "product_info": { "title": "Product Name" } } 

这是我的代码:

 using UnityEngine; using System.Collections; using System.IO; using System.Net; using UnityEngine.UI; public class ReadJson : MonoBehaviour { public Text myText; [System.Serializable] public class ProductInfo { public string title { get; set; } } [System.Serializable] public class RootObject { public ProductInfo product_info { get; set; } } void Start () { TextAsset asset = Resources.Load (Path.Combine ("Json", "toulouse")) as TextAsset; RootObject m = JsonUtility.FromJson (asset.text); Debug.Log (m.product_info.title); } } 

我收到此错误消息:“对象引用未设置为对象的实例”。 我已经尝试过,成功地解析了一个没有嵌套的json但是我不明白为什么但是在创建了适当的类之后仍然不起作用。

JsonUtility不支持属性。 只需删除{get; 组;}

 [System.Serializable] public class ProductInfo { public string title; } [System.Serializable] public class RootObject { public ProductInfo product_info; } 

Unity的JSON实现很像一个小孩子为他们的CS1项目写的东西。 对于任何严肃的JSON使用,它“缺乏”… 😉

推荐使用: JSON .NET For Unity,如果你可以为它付出代价。

或者……如果您希望坚持使用Unity的JSON实现,请使用https://github.com/Bekwnn/UnityJsonHelper 。 该库解决了您描述的确切问题。