步骤描述:
1.首先下载LitJson源码
下载地址:http://lbv.github.io/litjson/
2.LitJson在Unity3d中的配置
在Unity3d的assets中的plugins目录下新建一个文件夹litjson,然后把litjson的源代码中所有.cs文件放到这里。Unity3D文档描述中plugins目录中的脚本会先运行,这样保证在我们写其他脚本的时候,litjson已经加载并运行好了。
3.在脚本中引用litjson命名空间,using LitJson
例子解析:
- using UnityEngine;
- using System.Collections;
- using LitJson;
- public class UseJson : MonoBehaviour {
-
-
- void Start () {
- JsonData jsonData = new JsonData();
-
- jsonData["name"] = "wyz";
- jsonData["age"] = 28;
- jsonData["sex"] = "male";
- string strJson = jsonData.ToJson();
-
- Debug.Log(strJson);
-
-
- JsonData jsonData2 = JsonMapper.ToObject(strJson);
-
- Debug.Log(jsonData2["name"]);
-
-
- jsonData["info"] = new JsonData();
- jsonData["info"]["phone"] = 4424323;
- jsonData["info"]["address"] = "fuzhou";
- strJson = jsonData.ToJson();
-
- Debug.Log(strJson);
-
- JsonData jsonData3 = JsonMapper.ToObject(strJson);
-
- Debug.Log(jsonData3["info"]["phone"]);
-
- Debug.Log(jsonData3["info"][1]);
-
- Debug.Log(jsonData3["info"].Count);
-
- JsonData data = new JsonData ();
- data.Add (1);
- data.Add (2);
- data.Add (3);
- data.Add ("Launch!");
-
- jsonData["myArray"] = data;
- strJson = jsonData.ToJson();
-
- Debug.Log(strJson);
-
-
- for(int i=0;i<jsonData.Count;i++)
- {
- print(jsonData[i]);
- if(jsonData[i].IsArray)
- {
- print(jsonData[i][3]);
- }
- }
-
-
- }
-
- }
结果如下:

------------------------------------------------我是分割线---------------------------------------------------
处理JSON
5.解析JSON字符串显示字典键值
01 | public void ResolveJson() |
06 | ""Name"" : ""yusong"", |
08 | ""Birthday"" : ""1986-11-21"", |
24 | JsonData jd = JsonMapper.ToObject(str); |
25 | Debug.Log( "name = " + ( string )jd[ "Name" ]); |
26 | Debug.Log( "Age = " + ( int )jd[ "Age" ]); |
27 | Debug.Log( "Birthday = " + ( string )jd[ "Birthday" ]); |
28 | JsonData jdItems = jd[ "Thumbnail" ]; |
30 | for ( int i = 0; i < jdItems.Count; i++) |
32 | Debug.Log( "URL = " + jdItems[i][ "Url" ]); |
33 | Debug.Log( "Height = " + ( int )jdItems[i][ "Height" ]); |
34 | Debug.Log( "Width = " + jdItems[i][ "Width" ]); |
运行结果

6.合成JSON字符串,先合成 然后在输出。
01 | public void MergerJson() |
03 | StringBuilder sb = new StringBuilder (); |
04 | JsonWriter writer = new JsonWriter (sb); |
06 | writer.WriteObjectStart (); |
08 | writer.WritePropertyName ( "Name" ); |
09 | writer.Write ( "yusong" ); |
11 | writer.WritePropertyName ( "Age" ); |
14 | writer.WritePropertyName ( "Girl" ); |
16 | writer.WriteArrayStart (); |
18 | writer.WriteObjectStart(); |
19 | writer.WritePropertyName( "name" ); |
20 | writer.Write( "ruoruo" ); |
21 | writer.WritePropertyName( "age" ); |
23 | writer.WriteObjectEnd (); |
25 | writer.WriteObjectStart(); |
26 | writer.WritePropertyName( "name" ); |
28 | writer.WritePropertyName( "age" ); |
30 | writer.WriteObjectEnd (); |
32 | writer.WriteArrayEnd(); |
34 | writer.WriteObjectEnd (); |
35 | Debug.Log(sb.ToString ()); |
37 | JsonData jd = JsonMapper.ToObject(sb.ToString ()); |
38 | Debug.Log( "name = " + ( string )jd[ "Name" ]); |
39 | Debug.Log( "Age = " + ( int )jd[ "Age" ]); |
40 | JsonData jdItems = jd[ "Girl" ]; |
41 | for ( int i = 0; i < jdItems.Count; i++) |
43 | Debug.Log( "Girl name = " + jdItems[i][ "name" ]); |
44 | Debug.Log( "Girl age = " + ( int )jdItems[i][ "age" ]); |
运行结果

工程下载: http://vdisk.weibo.com/s/jkBml
首先注意头文件,LitJson是处理JSON的第三方库,最后我会给出下载地址。
2 | using System.Collections; |
3 | using System.Collections.Generic; |
1、生成XML
01 | public void createXml() |
04 | string filepath = Application.dataPath + @"/my.xml" ; |
06 | if (!File.Exists (filepath)) |
09 | XmlDocument xmlDoc = new XmlDocument(); |
11 | XmlElement root = xmlDoc.CreateElement( "transforms" ); |
13 | XmlElement elmNew = xmlDoc.CreateElement( "rotation" ); |
15 | elmNew.SetAttribute( "id" , "0" ); |
16 | elmNew.SetAttribute( "name" , "momo" ); |
18 | XmlElement rotation_X = xmlDoc.CreateElement( "x" ); |
20 | rotation_X.InnerText = "0" ; |
21 | XmlElement rotation_Y = xmlDoc.CreateElement( "y" ); |
22 | rotation_Y.InnerText = "1" ; |
23 | XmlElement rotation_Z = xmlDoc.CreateElement( "z" ); |
24 | rotation_Z.InnerText = "2" ; |
26 | rotation_Z.SetAttribute( "id" , "1" ); |
29 | elmNew.AppendChild(rotation_X); |
30 | elmNew.AppendChild(rotation_Y); |
31 | elmNew.AppendChild(rotation_Z); |
32 | root.AppendChild(elmNew); |
33 | xmlDoc.AppendChild(root); |
35 | xmlDoc.Save(filepath); |
36 | Debug.Log( "createXml OK!" ); |
运行结果
2 | <rotation id= "0" name= "momo" > |
2.更新XML文件
以其中某个节点名称做条件,当查询到时更新该节点
01 | public void UpdateXml() |
03 | string filepath = Application.dataPath + @"/my.xml" ; |
04 | if (File.Exists (filepath)) |
06 | XmlDocument xmlDoc = new XmlDocument(); |
08 | xmlDoc.Load(filepath); |
10 | XmlNodeList nodeList=xmlDoc.SelectSingleNode( "transforms" ).ChildNodes; |
12 | foreach (XmlElement xe in nodeList) |
15 | if (xe.GetAttribute( "id" )== "0" ) |
18 | xe.SetAttribute( "id" , "1000" ); |
20 | foreach (XmlElement x1 in xe.ChildNodes) |
25 | x1.InnerText= "update00000" ; |
32 | xmlDoc.Save(filepath); |
33 | Debug.Log( "UpdateXml OK!" ); |
运行结果
2 | <rotation id= "1000" name= "momo" > |
5 | <z id= "1" >update00000</z> |
3.添加XML
重复的地方我就不解释拉。
03 | string filepath = Application.dataPath + @"/my.xml" ; |
04 | if (File.Exists (filepath)) |
06 | XmlDocument xmlDoc = new XmlDocument(); |
07 | xmlDoc.Load(filepath); |
08 | XmlNode root = xmlDoc.SelectSingleNode( "transforms" ); |
09 | XmlElement elmNew = xmlDoc.CreateElement( "rotation" ); |
10 | elmNew.SetAttribute( "id" , "1" ); |
11 | elmNew.SetAttribute( "name" , "yusong" ); |
13 | XmlElement rotation_X = xmlDoc.CreateElement( "x" ); |
14 | rotation_X.InnerText = "0" ; |
15 | rotation_X.SetAttribute( "id" , "1" ); |
16 | XmlElement rotation_Y = xmlDoc.CreateElement( "y" ); |
17 | rotation_Y.InnerText = "1" ; |
18 | XmlElement rotation_Z = xmlDoc.CreateElement( "z" ); |
19 | rotation_Z.InnerText = "2" ; |
21 | elmNew.AppendChild(rotation_X); |
22 | elmNew.AppendChild(rotation_Y); |
23 | elmNew.AppendChild(rotation_Z); |
24 | root.AppendChild(elmNew); |
25 | xmlDoc.AppendChild(root); |
26 | xmlDoc.Save(filepath); |
27 | Debug.Log( "AddXml OK!" ); |
运行结果
02 | <rotation id= "1000" name= "momo" > |
05 | <z id= "1" >update00000</z> |
07 | <rotation id= "1" name= "yusong" > |
4.删除XML
01 | public void deleteXml() |
03 | string filepath = Application.dataPath + @"/my.xml" ; |
04 | if (File.Exists (filepath)) |
06 | XmlDocument xmlDoc = new XmlDocument(); |
07 | xmlDoc.Load(filepath); |
08 | XmlNodeList nodeList=xmlDoc.SelectSingleNode( "transforms" ).ChildNodes; |
09 | foreach (XmlElement xe in nodeList) |
11 | if (xe.GetAttribute( "id" )== "1" ) |
13 | xe.RemoveAttribute( "id" ); |
16 | foreach (XmlElement x1 in xe.ChildNodes) |
25 | xmlDoc.Save(filepath); |
26 | Debug.Log( "deleteXml OK!" ); |
运行结果
02 | <rotation id= "1000" name= "momo" > |
08 | <rotation name= "yusong" > |
4.解析与输出上面的XML
03 | string filepath = Application.dataPath + @"/my.xml" ; |
04 | if (File.Exists (filepath)) |
06 | XmlDocument xmlDoc = new XmlDocument(); |
07 | xmlDoc.Load(filepath); |
08 | XmlNodeList nodeList=xmlDoc.SelectSingleNode( "transforms" ).ChildNodes; |
10 | foreach (XmlElement xe in nodeList) |
12 | Debug.Log( "Attribute :" + xe.GetAttribute( "name" )); |
13 | Debug.Log( "NAME :" + xe.Name); |
14 | foreach (XmlElement x1 in xe.ChildNodes) |
18 | Debug.Log( "VALUE :" + x1.InnerText); |
23 | Debug.Log( "all = " + xmlDoc.OuterXml); |
运行结果(点击图片最大化)
