码迷,mamicode.com
首页 > Web开发 > 详细

JsonUtility一个小的注意事项

时间:2018-02-24 13:03:02      阅读:1387      评论:0      收藏:0      [点我收藏+]

标签:unity   JsonUtility   ToJson   重构   封装   

最近练习客户端服务端传对象,比如我们有个类

[Serializable]
public class Entity
{
    public string A;
    public string B;
}

上面这个是正确的
再转化json字符串

Entity e=new Entity();
e.A="A";
e.B="B";
JsonUtility.ToJson(e);

这样没问题,转化出来的json字符串是这样的:

{"A":"A", "B":"B"}

但一开始写了一个错误的Entity,传出的数据是空

[Serializable]
public class Entity
{
    private string a;
    private string b;
    public string A
        {
            get
            {
                return a;
            }

            set
            {
                a = value;
            }
        }

        public string B
        {
            get
            {
                return b;
            }

            set
            {
                b = value;
            }
        }
}

也就是说在VS里对这个两个属性进行了封装字段,封装后是这种带getter 和 setter的
这样转化出来的json字符串是:

{}

里面没有内容

写下来备忘

JsonUtility一个小的注意事项

标签:unity   JsonUtility   ToJson   重构   封装   

原文地址:http://blog.51cto.com/shuxiayeshou/2072571

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!