Unity Json
2015-11-16 19:26
411 查看
解析Json文件方法1:
1、Json格式:
{
"textfamily": [
{
"name": "tao1",
"age": 10
},
{
"name": "tao2",
"age": 20
}
]
}
2、解析Json步骤
(1) 读取Text文件:
TextAsset t = Resources.Load<TextAsset>("json/textJson");
(2)转化为存储对象的List
FamilyText s = JsonMapper.ToObject<FamilyText >(t.text);
(3)取出解析Json的数据,在对象属性上:
foreach (textData a in s.textfamily)
{
Debug.Log(a.name + "+" + a.age);
}
FamilyText 类:(List<textData>的变量名必须与Json中的对象名一致)
public class FamilyText
{
public List<textData> textfamily = new List<textData>();
}
textData类:
public class textData
{
public string name { get; set; }
public int age { get; set; }
}
-----------------------------------------------------------------------------------------------------------
解析Json文件方法2:
1、Json格式:
{
"people1": {
"name": "tao1",
"age": 10
},
"people2": {
"name": "tao2",
"age": 20
}
}
2、解析Json步骤
(1) 读取Text文件:
TextAsset t = Resources.Load<TextAsset>("json/textJson");
(2)转化为JsonNode对象:(总对象)
JSONNode data=JSON.Parse(t.text);
(3)取到people1,people2........的对象数组
JSONClass obj=data.AsObject;
(4)取到people1,people2.......对象
JSONClass sub1=obj[0] as JSONClass;
JSONClass sub2=obj[1] as JSONClass;
(5)取到对象中的key值:(如“name”....)
obj.key(0),obj.key(1).........
sub1.AsObject.key(0),sub1.AsObject.key(1)........
sub2.AsObject.key(0),sub2.AsObject.key(1).........
(6)取到对象中的value值:
sub[0].ToString(),sub[1].ToString................
--------------------------------------------------------------------------------------------------------
3、写入Json的方法
(1)声明一个需要存储的对象:
例如:
textData o =new textData ();
(2)为各属性赋值
o.name=""; .......
(3)转化为JSON
string json=JsonMapper.ToJson(o);
1、Json格式:
{
"textfamily": [
{
"name": "tao1",
"age": 10
},
{
"name": "tao2",
"age": 20
}
]
}
2、解析Json步骤
(1) 读取Text文件:
TextAsset t = Resources.Load<TextAsset>("json/textJson");
(2)转化为存储对象的List
FamilyText s = JsonMapper.ToObject<FamilyText >(t.text);
(3)取出解析Json的数据,在对象属性上:
foreach (textData a in s.textfamily)
{
Debug.Log(a.name + "+" + a.age);
}
FamilyText 类:(List<textData>的变量名必须与Json中的对象名一致)
public class FamilyText
{
public List<textData> textfamily = new List<textData>();
}
textData类:
public class textData
{
public string name { get; set; }
public int age { get; set; }
}
-----------------------------------------------------------------------------------------------------------
解析Json文件方法2:
1、Json格式:
{
"people1": {
"name": "tao1",
"age": 10
},
"people2": {
"name": "tao2",
"age": 20
}
}
2、解析Json步骤
(1) 读取Text文件:
TextAsset t = Resources.Load<TextAsset>("json/textJson");
(2)转化为JsonNode对象:(总对象)
JSONNode data=JSON.Parse(t.text);
(3)取到people1,people2........的对象数组
JSONClass obj=data.AsObject;
(4)取到people1,people2.......对象
JSONClass sub1=obj[0] as JSONClass;
JSONClass sub2=obj[1] as JSONClass;
(5)取到对象中的key值:(如“name”....)
obj.key(0),obj.key(1).........
sub1.AsObject.key(0),sub1.AsObject.key(1)........
sub2.AsObject.key(0),sub2.AsObject.key(1).........
(6)取到对象中的value值:
sub[0].ToString(),sub[1].ToString................
--------------------------------------------------------------------------------------------------------
3、写入Json的方法
(1)声明一个需要存储的对象:
例如:
textData o =new textData ();
(2)为各属性赋值
o.name=""; .......
(3)转化为JSON
string json=JsonMapper.ToJson(o);
相关文章推荐
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP中JSON的应用技巧
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析
- C#操作 JSON方法汇总
- JavaScript中字符串(string)转json的2种方法
- js Object2String方便查看js对象内容