当前位置:网站首页>C language parsing json string (json object is converted to string)
C language parsing json string (json object is converted to string)
2022-07-31 20:00:00 【Full stack programmer webmaster】
大家好,又见面了,我是你们的朋友全栈君.
如题,根据jsonThe difficulty of the string,C#The corresponding analysis methods provided are also different,Now do a simple classification summary:
一,对于最简单的json类型,可以使用如下方法
1.使用JsonReader类,示例代码如下
string meetingInfo = @”{“”meetingid”” : “”123″”, “”meetingname”” : “”abc””}”;
JsonReader reader = new JsonTextReader(new StringReader(meetingInfo)); while (reader.Read()) { Console.WriteLine(reader.TokenType + “\n” + reader.ValueType + “\n” + reader.Value); }
2.使用JObject类,示例代码如下:
string meetingInfo = @”{“”meetingid”” : “”123″”, “”meetingname”” : “”abc””}”;
JObject jm = JObject.Parse(meetingInfo); string[] meetings = jm.Properties().Select(item => item.Value.ToString()).ToArray();
二,对于复杂的json类型,使用如下方法
1.使用JArray类(Mainly for array formjson字符串),示例代码如下:
stringresultInfo = @”[{“id”:”123″,”name”:”abc”},{ “id”:”234″,”name”:”def”}]”;
JArray jf = (JArray)JsonConvert.DeserializeObject(resultInfo); fileid = jf[0][“id”].ToString(); filename = jf[0][“name”].ToString();
2.使用JObject类(mostly nested types),示例代码如下:
string resultInfo = @”{“meeting”:{“id”:”123″,”name”:”abc”}}”;
JObject jo = (JObject)JsonConvert.DeserializeObject(resultInfo);
string zone = jo[“meeting“][“id“].ToString(); string zone_en = jo1[“meeting“][“name“].ToString();
三,使用Newtonsoft.Json命名空间下的方法,示例代码如下:
private bool GetParameters() { try { var q = JsonConvert.DeserializeObject<ProtocolTO>(parameters); if (q == null) return false; id = q.id; action = q.action; fileid = q.fileid; meetingid = q.meetingid; type = q.type; sourceid = q.sourceid; status = q.status; } catch { return false; } return true; } public class ProtocolTO { public string id { get; set; } public string meetingid { get; set; } public string fileid { get; set; } public string sourceid { get; set; } public string app { get; set; } public string status { get; set; } public string action { get; set; } public string type { get; set; } }
附录
json格式:
1、对象是一个无序的“‘名称/值’对”集合. 例子:An object representing a person: {“姓名” : “abc”,”年龄” : “20”} 2、数组是值(value)的有序集合. 例子:学生对象 {“学生” :[{“姓名” : “小明” , “年龄” : “15”},{“姓名” : “tom” , “年龄” : “16”}]} json 对象数组:[{ “姓名” : “小明” , “年龄” : “15”},{ “姓名” : “tom” , “年龄” : “16”}] 3、值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array).这些结构可以嵌套. 4、字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127408.html原文链接:https://javaforall.cn
边栏推荐
- Thymeleaf是什么?该如何使用。
- 全平台GPU通用AI视频补帧超分教程
- MySQL---创建和管理数据库和数据表
- How can we improve the real yourself, become an excellent architect?
- &lt;artifactId&gt;ojdbc8&lt;/artifactId&gt;「建议收藏」
- BM5 merge k sorted linked lists
- 【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
- All-platform GPU general AI video supplementary frame super-score tutorial
- MySQL---单行函数
- ReentrantLock原理(未完待续)
猜你喜欢

MySQL - single function

基于STM32 环形队列来实现串口接收数据

NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores

MySQL - multi-table query

【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用

The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao

All-platform GPU general AI video supplementary frame super-score tutorial

Shell 脚本 快速入门到实战 -02
![leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]](/img/91/284de3dcbb8d143d85775b314dd41c.png)
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]

统计UTF-8字符串中的字符函数
随机推荐
ojdbc8 "Recommended Collection"性能优化:记一次树的搜索接口优化思路
MySQL---多表查询
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
pytorch lstm时间序列预测问题踩坑「建议收藏」
MySQL---聚合函数
Shell script quick start to actual combat -02
How can we improve the real yourself, become an excellent architect?
The principle of ReentrantLock (to be continued)
Basic Grammar Introduction of Carbon Tutorial (Tutorial)
linux查看redis版本命令(linux查看mysql版本号)
全网一触即发,自媒体人的内容分发全能助手——融媒宝
MySQL - multi-table query
老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
MySQL---operator
每月一书(202207):《Swift编程权威指南》
OSPFv3的基本配置
Istio介绍
请问我的这段sql中sql语法哪里出了错