当前位置:网站首页>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);
边栏推荐
- Timers, synchronous and asynchronous APIs, file system modules, file streams
- Easy-to-use translation plug-in - one-click automatic translation plug-in software
- 14. Thymeleaf
- 7. yaml
- EPro-PnP: Generalized End-to-End Probabilistic Perspective-n-Points for Monocular Object Pose Est...
- 【openpyxl】只读模式、只写模式
- [Excel知识技能] 将数值格式数字转换为文本格式
- How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
- Which foreign language journals and conferences can be submitted for software engineering/system software/programming language?
- How to recover data from accidentally deleted U disk, how to recover deleted data from U disk
猜你喜欢

地下管廊可视化管理系统搭建

16. File upload

2. Dependency management and automatic configuration

10. Notes on receiving parameters

input输入框超出部分用省略号表示以及判断内容是否有超出(PC端)

Why do programming languages have the concept of variable types?
![Which translation software is more accurate [Free]](/img/12/33d6724cfe8e8fe12a131c1e5e7a69.png)
Which translation software is more accurate [Free]

Which foreign language journals and conferences can be submitted for software engineering/system software/programming language?

英文文献阅读时,如何做笔记?

Promise in detail
随机推荐
16. File upload
13. 内容协商
如何做专利挖掘,关键是寻找专利点,其实并不太难
7. yaml
地下管廊可视化管理系统搭建
编程语言为什么有变量类型这个概念?
只会懒汉式和饿汉式 你还不懂单例模式!
electron -autoUpdater 更新
盘点美军的无人机家底
Analysis of LENS CRA and SENSOR CRA Matching Problems
7. yaml
How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
英文文献阅读时,如何做笔记?
学习Apache ShardingSphere解析器源码(一)
2. 依赖管理和自动配置
HGAME 2022 Week1 writeup
Introduction to Qt (6) - Implementation of the lottery system
PMP每日一练 | 考试不迷路-8.10(包含敏捷+多选)
Promise in detail
2022.8.10-----leetcode.640