当前位置:网站首页>c语言解析json字符串(json对象转化为字符串)
c语言解析json字符串(json对象转化为字符串)
2022-07-31 18:40:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
如题,根据json字符串的难易程度,C#提供的对应解析方法也不同,现做简单的归类总结:
一,对于最简单的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类(主要针对数组形式的json字符串),示例代码如下:
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类(主要是嵌套类型),示例代码如下:
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、对象是一个无序的“‘名称/值’对”集合。 例子:表示人的一个对象: {“姓名” : “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
边栏推荐
- [Network Communication 3] Advantech Gateway Modbus Service Settings
- 【码蹄集新手村600题】不通过字符数组来合并俩个数字
- 并发性,时间和相对性
- [TypeScript] OOP
- iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
- Kotlin coroutines: continuation, continuation interceptor, scheduler
- matplotlib ax bar color Set the color, transparency, label legend of the ax bar
- 【AcWing】第 62 场周赛 【2022.07.30】
- Masterless replication system (1) - write DB when node fails
- 35 MySQL interview questions and diagrams, this is also easy to understand
猜你喜欢

ThreadLocal

这位985教授火了!当了10年博导,竟无一博士毕业!

This 985 professor is on fire!After 10 years of Ph.D. supervisor, no one has graduated with a Ph.D.!

Golang——从入门到放弃
![[Network Communication 3] Advantech Gateway Modbus Service Settings](/img/ec/e9e1d9a374183ecaa8a8c9437ec82c.png)
[Network Communication 3] Advantech Gateway Modbus Service Settings

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

MySQL---运算符

35道MySQL面试必问题图解,这样也太好理解了吧

leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】

广汽本田安全体验营:“危险”是最好的老师
随机推荐
GAC Honda Safety Experience Camp: "Danger" is the best teacher
Golang 小数操作之判断几位小数点与四舍五入
35道MySQL面试必问题图解,这样也太好理解了吧
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
新型电信“套路”,我爸中招了!
广汽本田安全体验营:“危险”是最好的老师
This 985 professor is on fire!After 10 years of Ph.D. supervisor, no one has graduated with a Ph.D.!
Routing interception of WeChat applet
架构师04-应用服务间加密设计和实践
go记录之——slice
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
The article you worked so hard to write may not be your original
After Effects tutorial, How to adjust overexposed snapshots in After Effects?
基于WPF重复造轮子,写一款数据库文档管理工具(一)
多主复制下处理写冲突(3)-收敛至一致的状态及自定义冲突解决逻辑
Concurrency, Timing and Relativity
Flink_CDC搭建及简单使用
性能优化:记一次树的搜索接口优化思路
35 MySQL interview questions and diagrams, this is also easy to understand
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers