Unity读取Json数据的方式

版权声明:转载 请说明 https://blog.csdn.net/qq2512667/article/details/82055119
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LitJson;

public class myJson : MonoBehaviour {

	private TextAsset textasset;

	// Use this for initialization
	void Start () {
		//StartCoroutine(	readTextToFormJson ());
		readTextToFormJson ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void readTextToFormJson() {
		//一种读取本地工程中文件的方法
		textasset = Resources.Load ("book") as TextAsset;  //注意,这里文本文件的后缀txt不能写进去,否则读不出来
		JsonData jd;
		if (textasset != null) {
			jd = JsonMapper.ToObject (textasset.text);
		} else {
			return;
		}


		//几个特殊路径
//		Debug.Log ("dataPath :" + Application.dataPath);
//		Debug.Log ("persistentDataPath :" + Application.persistentDataPath);
//		Debug.Log ("temporaryCachePath :" + Application.temporaryCachePath);
//		Debug.Log ("streamingAssetsPath :" + Application.streamingAssetsPath);

		//第二种读取读取本地文件方法
//		FileStream fs = File.OpenRead (Application.dataPath+ "/Resources/book.txt");
//		StreamReader sr = new StreamReader (fs);
//		string wholeText = sr.ReadToEnd ();
//		sr.Close ();
//		fs.Close ();
//		fs.Dispose ();
//		JsonData jd = JsonMapper.ToObject (wholeText);

		//用WWW方法读取网络文件(加入协程)(可以做一个进度条)
//		WWW www = new WWW("http://9214193.s21d-9.faiusrd.com/68/ABUIABBEGAAg7YXmuAUohKPUtQY.txt");
//		Debug.Log ("start to load file.....");
//		yield return www;
//		Debug.Log ("end of load .");
//		JsonData jd = JsonMapper.ToObject (www.text);


		Debug.Log (jd.Count);
		Debug.Log (jd[0]);

//		读入json数据,并转化为程序中的数据结构
		List<bookItem> booklist = new List<bookItem> ();
		for (int i = 0; i < jd [0].Count; i++) {
			bookItem bitem = new bookItem ();
			bitem.name = jd [0] [i] ["bookname"].ToString();
			bitem.url = jd [0] [i] ["bookurl"].ToString();
			bitem.icon = jd [0] [i] ["icon"].ToString();
			booklist.Add (bitem);
		}

		foreach (bookItem item in booklist) {
			Debug.Log (item);
			Debug.Log (item.name);
		}

	}
}

//定义外部类,承载json数据
public class bookItem {
	public string name;
	public string url;
	public string icon;
}

 以前的知识  过久就会忘记,要多学习 多回顾才行啊。 这里的 JsonData对象 用Json映射后, 就成 了可操作的对象了,第一个层jd[0]是 json 最外层的,也是里面最大的"book_list":,第二层 jd[0][i]就是 book_list里面的{},{},{}。

第三层 jd[0][i]["bookname"]就是 存放的数据了。

{
	
	"book_list":
	[
	{	
			
		"bookname" : "story1",

		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book1.mp3",

		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book1.jpg"
	},


	{
		"bookname":"story3",
	
		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book3.mp3",
	
		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book8.jpg"
	},

	{	
			
		"bookname":"story3",
			
		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book3.mp3",
			
		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book8.jpg"
	},
	
	{	
		"bookname":"story3",
			
		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book3.mp3",

		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book10.jpg"
	}
	],
	"book_2t":
	[
	{	
			
		"bookname" : "story1",

		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book1.mp3",

		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book1.jpg"
	},


	{
		"bookname":"story3",
	
		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book3.mp3",
	
		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book8.jpg"
	},

	{	
			
		"bookname":"story3",
			
		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book3.mp3",
			
		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book8.jpg"
	},
	
	{	
		"bookname":"story3",
			
		"bookurl"  : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book3.mp3",

		"icon"     : "file://Users//Aikao//Desktop//Story//Unity_WorkSpace//book10.jpg"
	}
	]

}

另外 如果需要 用到int float这些, 只要用int.Parse(),float.Parse() 就可以了  

这种方法是 依赖于 ListJson的, Unity 官方 也提供了JsonUtility 只要using 一下就可以了

using UnityEngine;

[System.Serializable]
public class PlayerInfo
{
    public string name;
    public int lives;
    public float health;

    public static PlayerInfo CreateFromJSON(string jsonString)
    {
        return JsonUtility.FromJson<PlayerInfo>(jsonString);
    }

    // Given JSON input:
    // {"name":"Dr Charles","lives":3,"health":0.8}
    // this example will return a PlayerInfo object with
    // name == "Dr Charles", lives == 3, and health == 0.8f.
}

 如果 遇到 要读取 角色1  角色2 角色3 这样的数据时,  可能是还是需要ListJson吧

 还可以用这种方法 读取。。。PlayerList[] lists = JsonMapper.ToObject< PlayerList[]>(json);

方法如下  

using UnityEngine;
using LitJson;
[System.Serializable]
public class PlayerInfo
{
    public string name;
    public int lives;
    public float health;

 
    // Given JSON input:
    // {"name":"Dr Charles","lives":3,"health":0.8}
    // this example will return a PlayerInfo object with
    // name == "Dr Charles", lives == 3, and health == 0.8f.
}

public class PlayerList
{
 public List<PlayerInfo> playerList ;
}

public class Client:MonoBehaviour
{

    void Start
{
 PlayerList[] PlayerList=CreateFromJSON("Path");
}

   public static PlayerList[] CreateFromJSON(string jsonString)
    {
        return  PlayerList[] lists = JsonMapper.ToObject< PlayerList[]>(json);

    }

}

猜你喜欢

转载自blog.csdn.net/qq2512667/article/details/82055119