当前位置:网站首页>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
边栏推荐
- 架构实战营模块 8 作业
- 【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
- MySQL---子查询
- leetcode 665. Non-decreasing Array
- MySQL---sort and pagination
- Carbon教程之 基本语法入门大全 (教程)
- 高效并发:Synchornized的锁优化详解
- Three. Introduction to js
- Book of the Month (202207): The Definitive Guide to Swift Programming
- Given an ip address, how does the subnet mask calculate the network number (how to get the ip address and subnet mask)
猜你喜欢
C# 之 扑克游戏 -- 21点规则介绍和代码实现
学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
1161. 最大层内元素和 : 层序遍历运用题
高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
Made with Flutter and Firebase!counter application
All-platform GPU general AI video supplementary frame super-score tutorial
广汽本田安全体验营:“危险”是最好的老师
中文编码的设置与action方法的返回值
Short-circuit characteristics and protection of SiC MOSFETs
统计UTF-8字符串中的字符函数
随机推荐
MySQL---子查询
MySQL---sort and pagination
MySQL---聚合函数
ReentrantLock原理(未完待续)
leetcode 665. Non-decreasing Array 非递减数列(中等)
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
PCB stackup design
&lt;artifactId&gt;ojdbc8&lt;/artifactId&gt;「建议收藏」
"The core concept of" image classification and target detection in the positive and negative samples and understanding architecture
如何才能真正的提高自己,成为一名出色的架构师?
The old music player WinAmp released version 5.9 RC1: migrated to VS 2019, completely rebuilt, compatible with Win11
Book of the Month (202207): The Definitive Guide to Swift Programming
Financial profitability and solvency indicators
MySQL---多表查询
matplotlib ax bar color Set the color, transparency, label legend of the ax bar
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
财务盈利、偿债能力指标
Socket Review and I/0 Model
Three. Introduction to js