操作前安装LitJson:

将LitJson.dll文件添加到当前项目\bin\Debug文件夹中。

添加引用:在解决方案下找到引用,右击添加引用,浏览并添加上面的LitJson.dll文件。

导入命名空间:using LitJson;

解析

方法一:

using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class Program{static void Main(string[] args){ReadJson(CreateJson());Console.ReadKey();}static string CreateJson(){Person person1 = new Person() { theName = "张三", theAge = 15 };Person person2 = new Person() { theName = "李四", theAge = 25 };Persons persons = new Persons { persons = new Person[] { person1, person2 } };string jsonStr = JsonMapper.ToJson(persons);return jsonStr;}static void ReadJson(string jsonStr){Persons person = JsonMapper.ToObject<Persons>(jsonStr);foreach (Person item in person.persons){Console.WriteLine(item.theName);Console.WriteLine(item.theAge);}}}public class Person{public string theName;public int theAge;}public class Persons{public Person[] persons;}
}--->
张三
15
李四
25

方法二:

[
{"ID":1,"Name":"宋江","Chuohao":"及时雨","Xing":"天魁星"},
{"ID":2,"Name":"林冲","Chuohao":"豹子头","Xing":"天雄星"},
{"ID":3,"Name":"柴进","Chuohao":"小旋风","Xing":"天贵星"},
{"ID":4,"Name":"吴用","Chuohao":"智多星","Xing":"天机星"},
{"ID":5,"Name":"时迁","Chuohao":"鼓上蚤","Xing":"地贼星"}
]
using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test_CSharp
{class Program{static void Main(string[] args){JsonData jd = JsonMapper.ToObject(File.ReadAllText(@"C:\Users\86515\Desktop\TestJson.txt"));List<Hero> hl = new List<Hero>();int ID = 0;string name = null;string chuohao = null;string xing = null;foreach (JsonData item in jd){ID = int.Parse(item["ID"].ToString());name = item["Name"].ToString();chuohao = item["Chuohao"].ToString();xing = item["Xing"].ToString();Hero hero = new Hero() { _ID = ID, _name = name, _chuohao = chuohao, _xing = xing };hl.Add(hero);}foreach (var item in hl){Console.WriteLine("ID:{0},姓名:{1},绰号:{2},应星:{3}",item._ID, item._name, item._chuohao, item._xing);}Console.ReadKey();}}class Hero{public int _ID;public string _name;public string _chuohao;public string _xing;}
}--->
ID:1,姓名:宋江,绰号:及时雨,应星:天魁星
ID:2,姓名:林冲,绰号:豹子头,应星:天雄星
ID:3,姓名:柴进,绰号:小旋风,应星:天贵星
ID:4,姓名:吴用,绰号:智多星,应星:天机星
ID:5,姓名:时迁,绰号:鼓上蚤,应星:地贼星
{
"Name":"林冲",
"Chuohao":"豹子头",
"Xing":"天雄星",
"Story":
[
{"Storyid":1,"Storyname":"误入白虎堂"},
{"Storyid":2,"Storyname":"棒打洪教头"},
{"Storyid":3,"Storyname":"风雪山神庙"}
]
}
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test_CSharp
{class Program{static void Main(string[] args){JsonData jd1 = JsonMapper.ToObject(File.ReadAllText(@"C:\Users\86515\Desktop\TestJson2.txt"));List<Hero.Story> h = new List<Hero.Story>();string name = jd1["Name"].ToString();string chuohao = jd1["Chuohao"].ToString();string xing = jd1["Xing"].ToString();JsonData jd2 = jd1["Story"];foreach (JsonData itemm in jd2){string storyid = itemm["Storyid"].ToString();string storyname = itemm["Storyname"].ToString();Hero.Story story = new Hero.Story() { _storyid = storyid, _storyname = storyname };h.Add(story);}Hero hero = new Hero() { _name = name, _chuohao = chuohao, _xing = xing, _story = h };Console.WriteLine("姓名:{0},绰号:{1},应星:{2}", hero._name, hero._chuohao, hero._xing);foreach (Hero.Story itemm in hero._story){Console.WriteLine("故事ID:{0},故事名字:{1}", itemm._storyid, itemm._storyname);}Console.ReadKey();}}class Hero{public string _name;public string _chuohao;public string _xing;public List<Story> _story;public class Story{public string _storyid;public string _storyname;}}
}--->
姓名:林冲,绰号:豹子头,应星:天雄星
故事ID:1,故事名字:误入白虎堂
故事ID:2,故事名字:棒打洪教头
故事ID:3,故事名字:风雪山神庙
[{"Name":"林冲","Chuohao":"豹子头","Xing":"天雄星",
"Story":
[{"Storyid":1,"Storyname":"误入白虎堂"},{"Storyid":2,"Storyname":"棒打洪教头"},{"Storyid":3,"Storyname":"风雪山神庙"}]},
{"Name":"吴用","Chuohao":"智多星","Xing":"天机星",
"Story":
[{"Storyid":1,"Storyname":"智取生辰纲"},{"Storyid":2,"Storyname":"大破连环马"},{"Storyid":3,"Storyname":"三打祝家庄"}]}]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using LitJson;namespace Test_CSharp_Json
{class Program{static void Main(string[] args){//读取文件JsonData jsonData = JsonMapper.ToObject(File.ReadAllText(@"C:\Users\86515\Desktop\TestJson.txt"));//声明变量,接收数据string name;string chuohao;string xing;int storyID;string storyName;JsonData story;List<Heros> listHeros = new List<Heros>();            //遍历获取数据foreach (JsonData item in jsonData){name = item["Name"].ToString();chuohao = item["Chuohao"].ToString();xing = item["Xing"].ToString();story = item["Story"];List<Heros.Story> listStory = new List<Heros.Story>();foreach (JsonData ite in story){storyID =int.Parse(ite["Storyid"].ToString());storyName = ite["Storyname"].ToString();Heros.Story hstory = new Heros.Story() { _storyID = storyID, _storyName = storyName };listStory.Add(hstory);}Heros heros = new Heros() { _name = name, _chuohao = chuohao, _xing = xing, _listStory = listStory };listHeros.Add(heros);}foreach (var item in listHeros){Console.WriteLine("姓名:{0},绰号:{1},应星:{2}", item._name, item._chuohao, item._xing);foreach (var ite in item._listStory){Console.WriteLine("故事ID:{0},故事名字:{1}", ite._storyID, ite._storyName);}}}}class Heros{public string _name { get; set; }public string _chuohao { get; set; }public string _xing { get; set; }public List<Story> _listStory;public class Story{public int _storyID { get; set; }public string _storyName { get; set; }}}
}--->
姓名:林冲,绰号:豹子头,应星:天雄星
故事ID:1,故事名字:误入白虎堂
故事ID:2,故事名字:棒打洪教头
故事ID:3,故事名字:风雪山神庙
姓名:吴用,绰号:智多星,应星:天机星
故事ID:1,故事名字:智取生辰纲
故事ID:2,故事名字:大破连环马
故事ID:3,故事名字:三打祝家庄

创建

方法一:

using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test_CSharp
{class Program{static void Main(string[] args){         //写StringBuilder sb = new StringBuilder();JsonWriter writer = new JsonWriter(sb);writer.WriteObjectStart();writer.WritePropertyName("Name");writer.Write("林冲");writer.WritePropertyName("Chuohao");writer.Write("豹子头");writer.WritePropertyName("Xing");writer.Write("天雄星");writer.WritePropertyName("Story");writer.WriteArrayStart();writer.WriteObjectStart();writer.WritePropertyName("Storyid");writer.Write(1);writer.WritePropertyName("Storyname");writer.Write("误入白虎堂");writer.WriteObjectEnd();writer.WriteObjectStart();writer.WritePropertyName("Storyid");writer.Write(2);writer.WritePropertyName("Storyname");writer.Write("棒打洪教头");writer.WriteObjectEnd();writer.WriteObjectStart();writer.WritePropertyName("Storyid");writer.Write(3);writer.WritePropertyName("Storyname");writer.Write("风雪山神庙");writer.WriteObjectEnd();writer.WriteArrayEnd();writer.WriteObjectEnd();//读JsonData jd1 = JsonMapper.ToObject(sb.ToString());List<Hero.Story> h2 = new List<Hero.Story>();string name = jd1["Name"].ToString();string chuohao = jd1["Chuohao"].ToString();string xing = jd1["Xing"].ToString();JsonData jd2 = jd1["Story"];foreach (JsonData itemm in jd2){string storyid = itemm["Storyid"].ToString();string storyname = itemm["Storyname"].ToString();Hero.Story story = new Hero.Story() { _storyid = storyid, _storyname = storyname };h2.Add(story);}Hero hero = new Hero() { _name = name, _chuohao = chuohao, _xing = xing, _story = h2 };Console.WriteLine("姓名:{0},绰号:{1},应星:{2}", hero._name, hero._chuohao, hero._xing);foreach (Hero.Story itemm in hero._story){Console.WriteLine("故事ID:{0},故事名字:{1}", itemm._storyid, itemm._storyname);}Console.ReadKey();}}class Hero{public string _name;public string _chuohao;public string _xing;public List<Story> _story;public class Story{public string _storyid;public string _storyname;}}
}--->
姓名:林冲,绰号:豹子头,应星:天雄星
故事ID:1,故事名字:误入白虎堂
故事ID:2,故事名字:棒打洪教头
故事ID:3,故事名字:风雪山神庙

方法二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;namespace TestJsonwithunity
{class Program{static void Main(string[] args){Console.WriteLine(CreateJson());Console.ReadKey();}public static string CreateJson(){Person person1 = new Person() { theName = "张三", theAge = 15 };Person person2 = new Person() { theName = "李四", theAge = 25 };Persons persons = new Persons { persons = new Person[] { person1, person2 } };string jsonStr = JsonMapper.ToJson(persons);return jsonStr;}}public class Person{public string theName;public int theAge;}public class Persons{public Person[] persons;}
}--->
{"persons":[{"theName":"\u5F20\u4E09","theAge":15},{"theName":"\u674E\u56DB","theAge":25}]}

方法三:

单个对象

using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test_CSharp
{class Program{static void Main(string[] args){JsonData jsonData = new JsonData();jsonData["name"] = "Gavin";jsonData["age"] = 28;jsonData["sex"] = "male";string json = jsonData.ToJson();Console.WriteLine(json);Console.ReadKey();}}
}--->
{"name":"Gavin","age":28,"sex":"male"}

对象中包含对象

using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Test_CSharp
{class Program{static void Main(string[] args){JsonData jsonData = new JsonData();jsonData["name"] = "Gavin";jsonData["info"] = new JsonData();jsonData["info"]["age"] = 28;jsonData["info"]["sex"] = "male";string json = jsonData.ToJson();Console.WriteLine(json);Console.ReadKey();}}
}--->
{"name":"Gavin","info":{"age":28,"sex":"male"}}

对象与数组结合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;namespace TestJsonwithunity
{class Program{static void Main(string[] args){Console.WriteLine(CreateJson());Console.ReadKey();}static string CreateJson(){JsonData jsonData1 = new JsonData();jsonData1.SetJsonType(JsonType.Object);jsonData1["theName"] = "张三";jsonData1["theAge"] = 15;JsonData jsonData2 = new JsonData();jsonData2.SetJsonType(JsonType.Object);jsonData2["theName"] = "李四";jsonData2["theAge"] = 25;JsonData jsonDatas = new JsonData();jsonDatas.SetJsonType(JsonType.Array);jsonDatas.Add(jsonData1);jsonDatas.Add(jsonData2);JsonData jsonData = new JsonData();jsonData.SetJsonType(JsonType.Object);jsonData["Persons"] = jsonDatas;string jsonStr = jsonData.ToJson();return jsonStr;}}
}--->
{"Persons":[{"theName":"\u5F20\u4E09","theAge":15},{"theName":"\u674E\u56DB","theAge":25}]}

文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;namespace TestJsonwithunity
{class Program{static void Main(string[] args){string path = @"C:\Users\86515\Desktop\test.json";if (!File.Exists(path)){FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);    fs.Close();CreateJson(path);}else{CreateJson(path);}}static void CreateJson(string path){FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);JsonData jsonData1 = new JsonData();jsonData1.SetJsonType(JsonType.Object);jsonData1["theName"] = "张三";jsonData1["theAge"] = 15;JsonData jsonData2 = new JsonData();jsonData2.SetJsonType(JsonType.Object);jsonData2["theName"] = "李四";jsonData2["theAge"] = 25;JsonData jsonDatas = new JsonData();jsonDatas.SetJsonType(JsonType.Array);jsonDatas.Add(jsonData1);jsonDatas.Add(jsonData2);JsonData jsonData = new JsonData();jsonData.SetJsonType(JsonType.Object);jsonData["Persons"] = jsonDatas;string jsonStr = jsonData.ToJson();byte[] b = Encoding.UTF8.GetBytes(jsonStr);fs.Write(b, 0, b.Length);}}
}

C# 五十五、C#中使用Json相关推荐

  1. 哈哈日语 五十音图中发音最难的四个假名

    关于五十音图的发音,网上学习资源很多,就不一一解释了.这里只挑五十音图中发音最难把握的4个假名「え」「す」「つ」「ふ」来讲一讲. 首先,「え」(e)的发音,因为中文里有e这个音,所以很多人都会把「え」 ...

  2. Python编程基础:第五十五节 map函数Map

    第五十五节 map函数Map 前言 实践 前言 map函数的作用是将指定函数作用于一个可迭代对象内部的每一个元素,其表达方式为map(function, iterable),第一个位置指定作用函数,第 ...

  3. 【零基础学Java】—Socket类(五十五)

    [零基础学Java]-Socket类(五十五) Socket类:该类实现客户端套接字,套接字是指两台设备之间通讯的端点. 在Java中,提供了两个类用于实现TCP通信程序 客户端:java.net.S ...

  4. 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作

    科科过为您带来软考信息系统项目管理师核心重点考点(五十五)配置管理员(CMO)的工作,内含思维导图+真题 [信息系统项目管理师核心考点]配置管理员(CMO)的工作 1.制定配置管理计划 2.识别配置项 ...

  5. 我的世界服务器群系修改,我的世界创世神教程 第五十五节修改选区的生物群系|功能介绍|难点介绍|这节...

    我的世界WorldEdit创世神高级系列教程 第五十五节修改选区的生物群系.本教程由64条不同的技巧,功能介绍,难点介绍,防范措施介绍,工具介绍等组成.适合高级玩家和腐竹们来学习.这节内容给大家介绍修 ...

  6. 达芬奇密码 第五十五章 第五十六章

    达芬奇密码 第五十五章 第五十六章[@more@] 第五十五章 索菲靠着兰登坐在长沙发上,喝着茶吃着烤饼,享受着食物的美味.雷·提彬爵士微笑着,在炉火前面笨拙地踱来踱去.假肢敲在地面上,发出" ...

  7. [Python从零到壹] 五十五.图像增强及运算篇之图像平滑(均值滤波、方框滤波、高斯滤波)

    又是一年1024,首先,祝大家节日快乐! 欢迎大家来到"Python从零到壹",在这里我将分享约200篇Python系列文章,带大家一起去学习和玩耍,看看Python这个有趣的世界 ...

  8. 【Visual C++】游戏开发五十五 浅墨DirectX教程二十二 水乳交融的美学:alpha混合技术

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接: http://blog.csdn.net/poem_qianmo/article/details/15026917 作者:毛星 ...

  9. 要想做领导必须学会讲的五十五个故事

    一.用人之道 去过庙的人都知道,一进庙门,首先是弥陀佛,笑脸迎客,而在他的北面,则是黑口黑脸的韦陀.但相传在很久以前,他们并不在同一个庙里,而是分别掌管不同的庙. 弥乐佛热情快乐,所以来的人非常多,但 ...

  10. 小米note3 android 8,手机 篇五十五:红米Note8Pro拍照翻车,竟不敌两年前小米Note3!...

    手机 篇五十五:红米Note8Pro拍照翻车,竟不敌两年前小米Note3! 2020-01-29 06:10:14 5点赞 3收藏 5评论 正月初四,闲散在家,无聊至极,刷手机时无意中发现 DxOma ...

最新文章

  1. nix与linux的区别,linux – Nix / OS架构概述?
  2. Cucumber入门之_argument
  3. 软件测试c语言代码_软件测试理论知多少?
  4. 面试中关于多线程同步,你必须要思考的问题
  5. 实战: 如何掌握Oracle和业务IO知识
  6. 战网下载CDN重定向失败_卧槽,又开源一个下载神器,利用各种平台下载任意文件...
  7. netstat mysql_mysql-netstat
  8. *推荐* 杏雨梨云U盘系统2011 全面加速 [2010.12.28]
  9. 23. PHP include and require 文件
  10. Armadillo:踩坑指南(ubuntu-16.04+clion)
  11. 三菱Q系列ST、结构化编程、QD77定位以及转矩模式切换案例
  12. PS图层混合算法之五(饱和度,色相,颜色,亮度)
  13. Wex5修改Tomcat端口
  14. PNP与NPN两种三极管使用方法
  15. 关于mikefile 萌新用法
  16. Scratch软件编程等级考试三级——20210320
  17. HackTheBox::Bashed
  18. ESP32 快速入门(九):自定义 ESP32 分区表 partitions.csv
  19. 微软的应试题完整版(附答案)
  20. [原创]AHA大会回顾

热门文章

  1. python中done()是什么意思_turtle.done()的作用是什么
  2. 厨房净水器哪个牌子好,厨房净水器科普
  3. 安卓一键清理内存_雨点清理app下载-雨点清理 安卓版v1.0
  4. 如何设计出简洁实用的APP开发交互界面?
  5. 【关于IT专业怎么学】
  6. 支付宝发的计算机,支付宝电脑网站支付接口如何使用?
  7. 开发中常见的空指针异常场景及处理方案
  8. 一步步实现一个完整的万年历
  9. 快速入门Matlab——求取一元函数最小值和零点
  10. 魔兽争霸3宽屏显示器适配