首页 / UNITY / Unity 学习Json篇
Unity 学习Json篇
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Unity 学习Json篇,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4683字,纯文字阅读大概需要7分钟。
内容图文
![Unity 学习Json篇](/upload/InfoBanner/zyjiaocheng/1090/74f489e229274bfaaf098a53e79e1e1b.jpg)
介绍
JSON是一个简单的,但功能强大的序列化数据格式。它定义了简单的类型,如布尔,数(int和float)和字符串,和几个数据结构:list和dictionnary。可以在http://JSON.org了解关于JSON的更多信息。
litjson是用C #编写的,它的目的是要小,快速,易用。它使用了Mono框架。
安装LitJSON
将LitJSON编译好的dll文件通过Import New Asset的方式导入到项目中,再使用Using LitJSON即可使用JSONMapper类中的简便方法。dll的下载地址在这里.
将JSON转化为Object并可逆向转化
为了在.Net程序中使用JSON格式的数据。一个自然的方法是使用JSON文本生成一个特定的类的一个新实例;为了匹配类的格式,一般存储的JSON字符串是一个字典。
另一方面,为了将对象序列化为JSON字符串,一个简单的导出操作,听起来是个好主意。
为了这个目的,LitJSON包引入了JsonMapper类,它提供了两个用于做到 JSON转化为object 和 object转化为JSON 的主要方法。这两个方法是jsonmapper.toobject和jsonmapper.tojson。
将object转化为字符串之后,我们就可以将这个字符串很方便地在文件中读取和写入了。
一个简单的JsonMapper的例子
在下面的例子中,ToObject方法有一个泛型参数,来指定返回的某种特定的数据类型:即JsonMapper.ToObject<T>。
1 using LitJson; 2 using System; 3 4 public class Person 5 { 6 // C# 3.0 auto-implemented properties 7 public string Name { get; set; } 8publicint Age { get; set; } 9public DateTime Birthday { get; set; } 10} 1112publicclass JsonSample 13{ 14publicstaticvoid Main() 15 { 16 PersonToJson(); 17 JsonToPerson(); 18 } 1920publicstaticvoid PersonToJson() 21 { 22 Person bill = new Person(); 2324 bill.Name = "William Shakespeare"; 25 bill.Age = 51; 26 bill.Birthday = new DateTime(1564, 4, 26); 2728string json_bill = JsonMapper.ToJson(bill); 2930 Console.WriteLine(json_bill); 31 } 3233publicstaticvoid JsonToPerson() 34 { 35string json = @"36 { 37 ""Name"" : ""Thomas More"", 38 ""Age"" : 57, 39 ""Birthday"" : ""02/07/1478 00:00:00"" 40 }"; 4142 Person thomas = JsonMapper.ToObject<Person>(json); 4344 Console.WriteLine("Thomas‘ age: {0}", thomas.Age); 45 } 46 }
上文的输出:
1 {"Name":"William Shakespeare","Age":51,"Birthday":"04/26/1564 00:00:00"} 2 Thomas‘ age: 57
使用非泛型的JsonMapper.ToObject
当不存在特定的JSON数据类时,它将返回一个JSONData实例。JSONData是一种通用型可以保存任何数据类型支持JSON,包括list和dictionary。
1 using LitJson; 2 using System; 3 4 public class JsonSample 5 { 6 public static void Main() 7 { 8 string json = @" 9 { 10 ""album"" : { 11 ""name"" : ""The Dark Side of the Moon"", 12 ""artist"" : ""Pink Floyd"", 13 ""year"" : 1973, 14 ""tracks"" : [ 15 ""Speak To Me"", 16 ""Breathe"", 17 ""On The Run"" 18 ] 19 } 20 } 21"; 2223 LoadAlbumData(json); 24 } 2526publicstaticvoid LoadAlbumData(string json_text) 27 { 28 Console.WriteLine("Reading data from the following JSON string: {0}", 29 json_text); 3031 JsonData data = JsonMapper.ToObject(json_text); 3233// Dictionaries are accessed like a hash-table 34 Console.WriteLine("Album‘s name: {0}", data["album"]["name"]); 3536// Scalar elements stored in a JsonData instance can be cast to 37// their natural types 38string artist = (string) data["album"]["artist"]; 39int year = (int) data["album"]["year"]; 4041 Console.WriteLine("Recorded by {0} in {1}", artist, year); 4243// Arrays are accessed like regular lists as well 44 Console.WriteLine("First track: {0}", data["album"]["tracks"][0]); 45 } 46 }
上面例子的输出:
1 Reading data from the following JSON string: 2 { 3"album" : { 4"name" : "The Dark Side of the Moon", 5"artist" : "Pink Floyd", 6"year" : 1973, 7"tracks" : [ 8"Speak To Me", 9"Breathe", 10"On The Run"11 ] 12 } 13 } 1415 Album‘s name: The Dark Side of the Moon 16 Recorded by Pink Floyd in197317 First track: Speak To Me
Reader和Writer
一些人喜欢使用stream的方式处理JSON数据,对于他们, 我们提供的接口是jsonreader和jsonwriter。
JSONMapper实际上是建立在以上两个类的基础上的,所以你可以把这两个类当成是litJSON的底层接口。
使用JsonReader
1 using LitJson; 2 using System; 3 4 public class DataReader 5 { 6 public static void Main() 7 { 8 string sample = @"{ 9 ""name"" : ""Bill"", 10 ""age"" : 32, 11 ""awake"" : true, 12 ""n"" : 1994.0226, 13 ""note"" : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ] 14 }"; 1516 PrintJson(sample); 17 } 1819publicstaticvoid PrintJson(string json) 20 { 21 JsonReader reader = new JsonReader(json); 2223 Console.WriteLine ("{0,14} {1,10} {2,16}", "Token", "Value", "Type"); 24 Console.WriteLine (new String (‘-‘, 42)); 2526// The Read() method returns false when there‘s nothing else to read 27while (reader.Read()) { 28string type = reader.Value != null ? 29 reader.Value.GetType().ToString() : ""; 3031 Console.WriteLine("{0,14} {1,10} {2,16}", 32 reader.Token, reader.Value, type); 33 } 34 } 35 }
输出如下:
Token Value Type ------------------------------------------ ObjectStart PropertyName name System.String String Bill System.String PropertyName age System.String Int 32 System.Int32 PropertyName awake System.String Boolean True System.Boolean PropertyName n System.String Double 1994.0226 System.Double PropertyName note System.String ArrayStart String life System.String String is System.String String but System.String String a System.String String dream System.String ArrayEnd ObjectEnd
出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明
原文:http://www.cnblogs.com/AdvancePikachu/p/6515891.html
内容总结
以上是互联网集市为您收集整理的Unity 学习Json篇全部内容,希望文章能够帮你解决Unity 学习Json篇所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。