Unity中Json 解析(序列化和反序列化)

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

namespace SolJSONExample
{
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Company
    {
        public Person CEO { get; set; }
        public List<Person> Staffs { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            {
                string person_info_str = "{\"Name\":\"admin1\", \"Age\": 13}";
                Person person_info = SolJSON.Convert.JsonConverter.ToObject<Person>(person_info_str);

                Console.WriteLine("Name = " + person_info.Name);
                Console.WriteLine("Age = " + person_info.Age);
            }
            {
                string person_infos_str = "[{\"Name\":\"admin1\", \"Age\": 13},{\"Name\":\"admin2\", \"Age\": 20}]";
                List<Person> person_infos = SolJSON.Convert.JsonConverter.ToObject<List<Person>>(person_infos_str);

                foreach (var person_info in person_infos)
                {
                    Console.WriteLine("Name = " + person_info.Name);
                    Console.WriteLine("Age = " + person_info.Age);
                }
            }
            {
                string company_str = "{\"CEO\":{\"Name\":\"admin1\", \"Age\": 13}, \"Staffs\": [{\"Name\":\"admin2\", \"Age\": 25}, {\"Name\":\"admin3\", \"Age\": 20}]}";
                Company company = SolJSON.Convert.JsonConverter.ToObject<Company>(company_str);

                Console.WriteLine("CEO.Name = " + company.CEO.Name);
                Console.WriteLine("Staffs");

                foreach (var staff in company.Staffs)
                {
                    Console.WriteLine("Name = " + staff.Name);
                    Console.WriteLine("Age = " + staff.Age);
                }
            }
        }
    }
}


字符串转化为json数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SolJSONExample
{
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person()
            {
                Name = "admin1",
                Age = 16
            };

            Console.WriteLine("json string = " + SolJSON.Convert.JsonConverter.ToJSON(person));
        }
    }
}


取出Json数据中的某一个元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SolJSONExample
{
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            {
                string person_info_str = "{\"Name\":\"admin1\", \"Age\": 13}";
                SolJSON.Types.JsonObject person_info_json_object = SolJSON.Convert.JsonConverter.ToJsonObject(person_info_str);

                if ( person_info_json_object.Type == SolJSON.Types.JsonObject.TYPE.DICTONARY )
                {
                    if ( person_info_json_object.AsDictonary.Contains("Name") )
                    {
                        Console.WriteLine("Name = " + person_info_json_object.AsDictonary["Name"].AsString);
                    }
                }
            }
            {
                string person_infos_str = "[{\"Name\":\"admin1\", \"Age\": 13},{\"Name\":\"admin2\", \"Age\": 20}]";
                SolJSON.Types.JsonObject person_infos_json_object = SolJSON.Convert.JsonConverter.ToJsonObject(person_infos_str);

                foreach (var person_info in person_infos_json_object.AsArray)
                {
                    Console.WriteLine("Name = " + person_info.AsDictonary["Name"].AsString);
                    Console.WriteLine("Age = " + person_info.AsDictonary["Age"].AsNumber.IntValue);
                }
            }
        }
    }
}


Json字典
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SolJSONExample
{

    class Program
    {
        static void Main(string[] args)
        {
            SolJSON.Types.JsonDictonary person_dict = new SolJSON.Types.JsonDictonary();
            person_dict.Add("Name", new SolJSON.Types.JsonString("admin1"));
            person_dict.Add("Age", new SolJSON.Types.JsonNumber(13));

            Console.WriteLine("json string = " + person_dict.ToString());
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页