当前位置:网站首页>C# JObject解析JSON数据
C# JObject解析JSON数据
2022-08-11 00:09:00 【596785154】
1.引用DLL文件
在项目中引用Newtonsoft.Json.dll
2.解析简单json数据
//生成json类型数据
JObject obj = new JObject();
obj.Add("key1", "value1");
obj.Add("key2", "value2");
obj.Add("key3", 3);
String response = obj.ToString();
Console.WriteLine(response);
Console.WriteLine("=================================");
//解析数值
JObject joResponse = (JObject)JsonConvert.DeserializeObject(response);
Console.WriteLine(joResponse["key1"].ToString());
Console.WriteLine(joResponse["key2"].ToString());
Console.WriteLine(joResponse["key3"].ToString());
3.解析json数组
//生成数组
JArray jarray = new JArray();
JObject jobj = new JObject();
jobj.Add("name", "张三");
jobj.Add("age", 18);
jobj.Add("sex", "男");
JObject jobj2 = new JObject();
jobj2.Add("name", "李四");
jobj2.Add("age", 18);
jobj2.Add("sex", "女");
jarray.Add(jobj);
jarray.Add(jobj2);
String response = jarray.ToString();
Console.WriteLine(response);
Console.WriteLine("============================================");
//解析数组
JArray ja = (JArray)JsonConvert.DeserializeObject(response);
//获取数值
foreach (JObject jo in ja)
{
string name = jo["name"].ToString();
string age = jo["age"].ToString();
string sex = jo["sex"].ToString();
Console.WriteLine("name="+name + ", age=" + age+", sex=" + sex);
}
//单个数值获取
JObject joo = (JObject)ja[1];
string name2 = joo["name"].ToString();
Console.WriteLine("name2="+name2);
string age2 = joo["age"].ToString();
Console.WriteLine("age2=" + age2);
string sex2 = joo["sex"].ToString();
Console.WriteLine("sex2=" + sex2);
边栏推荐
- 图片懒加载(纯手写)
- 【考虫 六级英语】语法课笔记
- [C Language Chapter] Detailed explanation of bitwise operators (“<<”, “>>”, “&”, “|”, “^”, “~”)
- 分布式.性能优化
- UOJ#749-[UNR #6]稳健型选手【贪心,分治,主席树】
- The Missing Semester of Your CS Education
- 2022下半年软考「高项」易混淆知识点汇总(2)
- ADC和DAC记录
- 软件测试证书(1)—— 软件评测师
- Promote the high-quality development of denim clothing
猜你喜欢
随机推荐
Ali P7 bask in January payroll: hard to fill the, really sweet...
分库分表ShardingSphere-JDBC笔记整理
两个链表的第一个公共节点——LeetCode
[Excel知识技能] 将“假“日期转为“真“日期格式
如何破坏Excel文件,让其显示文件已损坏方法
CF1534F2-Falling Sand (Hard Version)
Web-based meal ordering system in epidemic quarantine area
SAS数据处理技术(一)
sqlmap结合dnslog快速注入
【经典排序】快速排序
How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
11. 自定义转换器
Pagoda Test-Building PHP Online Mock Exam System
[Excel knowledge and skills] Convert "false" date to "true" date format
如何做专利挖掘,关键是寻找专利点,其实并不太难
Easy-to-use translation plug-in - one-click automatic translation plug-in software
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
力扣每日一题-第52天-387. 字符串中的第一个唯一字符
给肯德基打工的调料商,年赚两亿
线上突然查询变慢怎么核查