C#의 Json 작업 Json Mapper 분석

14561 단어 C#
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();
        }
    }
}

좋은 웹페이지 즐겨찾기