unity中使用Litjson解析json

本文介绍了一种使用Unity3D进行游戏开发时,如何解析JSON数据的方法。通过两种方式展示了解析过程:一是使用实体类接收JSON数据,二是使用JsonData接收并解析JSON数据。文章详细说明了如何读取JSON文件,将数据映射到实体类或JsonData中,并展示了如何获取视频ID、标题等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

被解析的json

{
	"video": {
		"id": "29BA6ACE7A9427489C33DC5901307461",
		"title": "体验课01",
		"desp": "不可描述",
		"tags": "noTags",
		"duration": 503,
		"category": "07AD1E11DBE6FDFC",
		"image": "http://2.img.bokecc.com/comimage/0DD1F081022C163E/2016-0309/29BA6ACE7A9427489C33DC5901307461-0.jpg",
		"imageindex": 0,
		"imagealternate": [
			{
				"index": 0,
				"url": "http://2.img.bokecc.com/comimage/0DD1F081022C163E/2016-03-09/29BA6ACE7A9427489C33DC5901307461-0/0.jpg"
			},
			{
				"index": 1,
				"url": "http://2.img.bokecc.com/comimage/0DD1F081022C163E/2016-03-09/29BA6ACE7A9427489C33DC5901307461-0/1.jpg"
			},
			{
				"index": 2,
				"url": "http://2.img.bokecc.com/comimage/0DD1F081022C163E/2016-03-09/29BA6ACE7A9427489C33DC5901307461-0/2.jpg"
			},
			{
				"index": 3,
				"url": "http://2.img.bokecc.com/comimage/0DD1F081022C163E/2016-03-09/29BA6ACE7A9427489C33DC5901307461-0/3.jpg"
			}
		]
	}
}

方法 一

//用实体类接收json数据
private void ParseJson()
	{
        //自己json文件路径
		string path = Application.dataPath+"/tempJson.json";
		if (File.Exists(path))
		{
			StreamReader sr = new StreamReader(path);
			string jsonText = sr.ReadToEnd();
			sr.Close();
			Root root = JsonMapper.ToObject<Root>(jsonText);
			Video video = root.video;
			print("视频id:" + video.id + "  视频标题:" + video.title);
		}
	}


//******************json对应的实体类*********************
	public class Imagealternate
	{
		public int index { get; set; }
		public string url { get; set; }
	}

	public class Video
	{
		public string id { get; set; }
		public string title { get; set; }
		public string desp { get; set; }
		public string tags { get; set; }
		public int duration { get; set; }
		public string category { get; set; }
		public string image { get; set; }
		public int imageindex { get; set; }
		public List<Imagealternate> imagealternate { get; set; }
	}

	public class Root
	{
		public Video video { get; set; }
	}

方法二 

//用JsonData接收json数据
private void ParseJson()
	{
        //自己json文件路径
		string path = Application.dataPath + "/tempJson.json";
		if (File.Exists(path))
		{
			StreamReader sr = new StreamReader(path);
			string jsonText = sr.ReadToEnd();
			sr.Close();
			JsonData jsonData = JsonMapper.ToObject(jsonText);
			JsonData video = jsonData["video"];
			string id=  video["id"].ToString();
			string title = video["title"].ToString();
			print("视频id:" + id + "  视频标题:" + title);
			JsonData imagealternateList = video["imagealternate"];
			print("数量:" + imagealternateList.Count);
			for(int i=0;i<imagealternateList.Count;i++)
			{
				int index = int.Parse( imagealternateList[i]["index"].ToString());
				string url = imagealternateList[i]["url"].ToString();
				print("索引:" + index + "  url:" + url);

			}
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值