JSON中使用jsonmapper解析的代码和步骤 学习笔记

代码:

using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JSON操作
{
class Program
{
static void Main(string[] args)
{
//7.创建一个集合来储存我们遍历的三个对象并添加到这个集合中
List<Skill> skillList = new List<Skill>();
//1.我们使用JsonMapper去解析文本
//2.再使用ToObject方法去传入需要解析的文本
//3.然后使用File.ReadAllText()方法传入需要传入的文本信息 最后返回一个JsonData类型的数组或者对象
JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json技能信息.txt"));
//4.然后遍历我们返回的这个数组或者对象
foreach (JsonData temp in jsonData) //这里面的temp代表每一个json中的一个对象{}这个值
{
//8.为了增加这个遍历以后的对象我们必须先创建一个这样的对象然后为每个对象中的属性赋值
Skill skill = new Skill();
//5.然后我们通过对象.["访问的对象属性的键"]这样的方式来访问他对应的值
JsonData idValue = temp["id"];
JsonData nameValue = temp["name"];
JsonData damageValue = temp["damage"];
//6.接着就可以将得到值通过转化来去的能够在控制台中输出的值
int id = Int32.Parse(idValue.ToString());
int damage = Int32.Parse(damageValue.ToString());
//9.然后为对象的每一个属性赋值
skill.id = id;
skill.name = nameValue.ToString();
skill.damage = damage;

//10.然后让skillList这个集合去添加这个对象
skillList.Add(skill);

//Console.WriteLine(id+":"+nameValue+":"+damage);

}
//11.然后遍历集合中的对象
foreach (var item in skillList)
{
Console.WriteLine(item);
}

Console.ReadKey();
}
}
}

原文地址:https://www.cnblogs.com/ylllove/p/6885859.html