当前位置:网站首页>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
边栏推荐
猜你喜欢
Teach you how to deploy Nestjs projects
flowable工作流所有业务概念
【码蹄集新手村600题】通向公式与程序相结合
All-platform GPU general AI video supplementary frame super-score tutorial
MySQL - single function
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
35道MySQL面试必问题图解,这样也太好理解了吧
Three. Introduction to js
GAC Honda Safety Experience Camp: "Danger" is the best teacher
随机推荐
中文编码的设置与action方法的返回值
1161. 最大层内元素和 : 层序遍历运用题
基于WPF重复造轮子,写一款数据库文档管理工具(一)
idea中搜索具体的字符内容的快捷方式
深度学习中的batch(batch size,full batch,mini batch, online learning)、iterations与epoch
常用的安全渗透测试工具(渗透测试工具)
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
matplotlib ax bar color 设置ax bar的颜色、 透明度、label legend
如何才能真正的提高自己,成为一名出色的架构师?
MySQL---基本的select语句
Golang 切片删除指定元素的几种方法
AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
How programmers learn open source projects, this article tells you
使用 Flutter 和 Firebase 制作!计数器应用程序
Three.js入门
Apache EventMesh 分布式事件驱动多运行时
Made with Flutter and Firebase!counter application
Given an ip address, how does the subnet mask calculate the network number (how to get the ip address and subnet mask)
UserAgent 解析
How can we improve the real yourself, become an excellent architect?