C#之Json操作JsonMapper解析

json技能信息

[
{"id":2,"name":"天下无双","damage":123 },
{"id":3,"name":"天下无贼","damage":21 },
{"id":4,"name":"咫尺天涯","damage":900 }
]

json主角信息

{
	"Name":"siki",
	"Level":99,
	"Age":18,
	"SkillList":[
		{"id":2,"name":"天下无双","damage":123 },
		{"id":3,"name":"天下无贼","damage":21 },
		{"id":4,"name":"咫尺天涯","damage":900 }
	]
}

Skill类、Player类

namespace _032_json操作 {
    class Skill
    {
        public int id;
        public int damage;
        public string name;

        public override string ToString()
        {
            return string.Format("Id: {0}, Damage: {1}, Name: {2}", id, damage, name);
        }
    }
    class Player
    {
    //    public string name;//字段或者属性名 要跟json里面的对应
    //    public int level;
        public string Name { get; set; }
        public int Level { get; set; }
        public int Age { get; set; }
        public List<Skill> SkillList { get; set; }

        public override string ToString()
        {
            return string.Format("Name: {0}, Level: {1}, Age: {2}, SkillList: {3}", Name, Level, Age, SkillList);
        }
    }

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

namespace _032_json操作 {
    class Program {
        static void Main(string[] args) {
            //使用litjson进行解析json文本
            //两种引入litjson的方法
            //1,去litjson的网站下载litjson.dll 然后添加引用 找到dll所在目录
            //2,右键引用 打开管理netget程序包,在联机里面搜索litjson  在搜索结果中选择一个 点击安装

            //List skillList = new List();
            //我们使用jsonMapper去解析json文本
            //jsondata代表一个数组或者一个对象
            //在这里jsonData代表数组 
            //JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json技能信息.txt"));
            //foreach (JsonData temp in jsonData)//在这里temp代表一个对象
            //{
            //    Skill skill = new Skill();
            //    JsonData idValue =temp["id"]; //通过字符串索引器可以取得键值对的值
            //    JsonData nameValue = temp["name"];
            //    JsonData damageValue = temp["damage"];
            //    int id = Int32.Parse(idValue.ToString());
            //    int damage = Int32.Parse(damageValue.ToString());
            //    skill.id = id;
            //    skill.damage = damage;
            //    skill.name = nameValue.ToString();
            //    skillList.Add(skill);
            //}
            //foreach (var temp in skillList)
            //{
            //    Console.WriteLine(temp);
            //}

            //使用泛型去解析json
            //json里面对象的键必须跟定义的类里面的字段或者属性保持一致
            //Skill[] skillArray= JsonMapper.ToObject(File.ReadAllText("json技能信息.txt"));
            //foreach (var temp in skillArray)
            //{
            //    Console.WriteLine(temp);
            //}
            //List skillList = JsonMapper.ToObject>(File.ReadAllText("json技能信息.txt"));
            //foreach (var temp in skillList) {
            //    Console.WriteLine(temp);
            //}

            //Player p= JsonMapper.ToObject(File.ReadAllText("主角信息.txt"));
            //Console.WriteLine(p);
            //foreach (var temp in p.SkillList)
            //{
            //    Console.WriteL ine(temp);
            //}

            //Player p = new Player();
            //p.Name = "花千骨";
            //p.Level = 100;
            //p.Age = 16;
            //string json =JsonMapper.ToJson(p);
            //Console.WriteLine(json);
            Console.ReadKey();
        }
    }
}

你可能感兴趣的:(C#)