统一解析嵌套的json [英] Parse nested json in unity

查看:112
本文介绍了统一解析嵌套的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析此json时遇到问题:

I have an issue parsing this 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<RootObject> (asset.text);

        Debug.Log (m.product_info.title);

    }
}

我收到此错误消息:对象引用未设置为对象的实例".我已经尝试过,成功解析了一个非嵌套的json,但是我不明白为什么,但是即使创建了适当的类也无法正常工作.

I receive this error message : "Object reference not set to an instance of an object". I already tried, with success to parse a not nested json but not I don't understand why but doesn't work even after created the appropriated class.

推荐答案

JsonUtility不支持属性.只需删除{get;设置;}

The JsonUtility does not support properties. Just remove the { get; set;}

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

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

这篇关于统一解析嵌套的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆