当前位置:网站首页>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
边栏推荐
- What's wrong with the sql syntax in my sql
- NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
- Linux环境redis集群搭建「建议收藏」
- UVM RAL模型和内置seq
- NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品
- ResNet的基础:残差块的原理
- 移动web开发02
- 高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询
- Socket回顾与I/0模型
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
猜你喜欢

Realization of character makeup

idea中搜索具体的字符内容的快捷方式

财务盈利、偿债能力指标

高通cDSP简单编程例子(实现查询高通cDSP使用率、签名),RK3588 npu使用率查询

如何才能真正的提高自己,成为一名出色的架构师?

Socket回顾与I/0模型

Introduction to Audio Types and Encoding Formats in Unity

MATLAB程序设计与应用 2.4 MATLAB常用内部函数

Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...

Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
随机推荐
全平台GPU通用AI视频补帧超分教程
NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
求n以内的素数
Shell 脚本 快速入门到实战 -02
API for JD.com to obtain historical price information of commodities
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
Book of the Month (202207): The Definitive Guide to Swift Programming
All-platform GPU general AI video supplementary frame super-score tutorial
MySQL - multi-table query
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
Apache EventMesh 分布式事件驱动多运行时
Socket回顾与I/0模型
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
架构实战营模块八作业
请问我的这段sql中sql语法哪里出了错
每月一书(202207):《Swift编程权威指南》
uni-app中的renderjs使用
matplotlib ax bar color 设置ax bar的颜色、 透明度、label legend
【愚公系列】2022年07月 Go教学课程 025-递归函数