public class JsonHelper
{
public static void CreateJson()
{
JObject jsObj = new JObject();
jsObj.Add("career", "hunter");
JObject jsSystem = new JObject();
jsSystem.Add("name", "name");
jsSystem.Add("count", 999);
jsSystem.Add("unique", false);
JArray jsArraySys0 = new JArray();
JObject jsSys1 = new JObject();
jsSys1.Add("system_name", "强化系");
JArray jsArraySys1 = new JArray();
jsArraySys1.Add(new JValue("Gon"));
jsArraySys1.Add(new JValue("雷欧力"));
jsArraySys1.Add(new JValue("窝金"));
jsSys1.Add("character", jsArraySys1);
jsArraySys0.Add(jsSys1);
JObject jsSys2 = new JObject();
jsSys2.Add("system_name", "变化系");
JArray jsArraySys2 = new JArray();
jsArraySys2.Add(new JValue("Killua"));
jsArraySys2.Add(new JValue("西索"));
jsSys2.Add("character", jsArraySys2);
jsArraySys0.Add(jsSys2);
JObject jsSys3 = new JObject();
jsSys3.Add("system_name", "操作系");
JArray jsArraySys3 = new JArray();
jsArraySys3.Add(new JValue("侠客"));
jsArraySys3.Add(new JValue("半藏"));
jsSys3.Add("character", jsArraySys3);
jsArraySys0.Add(jsSys3);
JObject jsSys4 = new JObject();
jsSys4.Add("system_name", "具现化系");
JArray jsArraySys4 = new JArray();
jsArraySys4.Add(new JValue("Kula"));
jsArraySys4.Add(new JValue("库哔"));
jsArraySys4.Add(new JValue("小嘀"));
jsSys4.Add("character", jsArraySys4);
jsArraySys0.Add(jsSys4);
JObject jsSys5 = new JObject();
jsSys5.Add("system_name", "放出系");
JArray jsArraySys5 = new JArray();
jsArraySys5.Add(new JValue("富兰克林"));
jsArraySys5.Add(new JValue("旋律"));
jsArraySys5.Add(new JValue("爆弹魔"));
jsSys5.Add("character", jsArraySys5);
jsArraySys0.Add(jsSys5);
JObject jsSys6 = new JObject();
jsSys6.Add("system_name", "特质系");
JArray jsArraySys6 = new JArray();
jsArraySys6.Add(new JValue("Kula-火红眼"));
jsArraySys6.Add(new JValue("库洛洛"));
jsArraySys6.Add(new JValue("派克诺妲"));
jsSys6.Add("character", jsArraySys6);
jsArraySys0.Add(jsSys6);
jsSystem.Add("system", jsArraySys0);
jsObj.Add("system", jsSystem);
string strSrc = Convert.ToString(jsObj);//将json装换为string
File.WriteAllText(Directory.GetCurrentDirectory() + "\\test.json", strSrc, System.Text.Encoding.UTF8);//将内容写进json文件
}
public static void ReadJson()
{
string folder = Directory.GetCurrentDirectory(); //获取应用程序的当前工作目录。
string path = folder + "\\test.json";
StreamReader file = File.OpenText(path);
JsonTextReader reader = new JsonTextReader(file);
JObject jsonObject = (JObject)JToken.ReadFrom(reader);
string career = jsonObject["career"].ToString();
int nCount = int.Parse(jsonObject["system"]["count"].ToString());
bool unique = bool.Parse(jsonObject["system"]["unique"].ToString());
string name = jsonObject["system"]["system"][0]["system_name"].ToString();
}
public static void JsonSerialize()
{
string folder = Directory.GetCurrentDirectory(); //获取应用程序的当前工作目录。
string path = folder + "\\test1.json";
Student student = new Student() { Age =18,Birthday="20020202",Name="张三"};
string result = JsonConvert.SerializeObject(student);
using (StreamWriter file = new StreamWriter(path))
{
file.Write(result);
}
}
public static void JsonDeSerialize()
{
string folder = Directory.GetCurrentDirectory(); //获取应用程序的当前工作目录。
string path = folder + "\\test1.json";
string content = "";
using (StreamReader streamReader = new StreamReader(path))
{
content = streamReader.ReadToEnd();
}
Student student = JsonConvert.DeserializeObject<Student>(content);
}
}
public class Student
{
public int Age { get; set; }
public string Birthday { get; set;}
public string Name { get; set; }
}