C#下JSON字符串的反序列化
发布日期:2021-06-30 17:22:56 浏览次数:2 分类:技术文章

本文共 1599 字,大约阅读时间需要 5 分钟。

C#下JSON字符串的反序列化,一般都是用newtonsoft.json,比较方便。.net当然也有提供相应功能,但觉得比较复杂。

所谓反序列化,就是将一个包含JSON内容的字符串,转换回指定对象(不一定是转换回JSON对象)。

方法是:

using Newtonsoft.Json;

。。。

JsonConvert.DeserializeObject<。。。>(strJson)

示例1:

 

public class ViewTag{    public int ViewId { get; set; }    public string Name { get; set; }    public bool IsValid { get; set; }    public int Seq { get; set; }    public byte ChangeType { get; set; }}string strJson = "[{'ViewId':14,'Name':'view 1','IsValid':true,'Seq':'1','ChangeType':0},{'ViewId':15,'Name':'view 2','IsValid':true,'Seq':'2','ChangeType':0}]";List
list = JsonConvert.DeserializeObject
>(strJson);

上述例子中,json字符串内,每个json元素的数据结构都一样。但假如不一样,怎么办?

 

示例2:

 

string strJson = @"[{	'id':1,	'text':'All Object',	'iconCls':'', 	'children':[{		'id':11,		'text':'Key attributes',		'children':[{			'id':111,			'text':'Item1'		},{			'id':112,			'text':'Item2'		},{			'id':113,			'text':'Item3'		}]	},{		'id':12,		'text':'Service table 1',		'state':'closed',		'children':[{			'id':121,			'text':'Service 1'		},{			'id':122,			'text':'Service 2'		},{			'id':123,			'text':'Service 3'		}]	},{		'id':13,		'text':'Service table 2',		'state':'closed',		'children':[{			'id':131,			'text':'Service 1'		},{			'id':132,			'text':'Service 2'		},{			'id':133,			'text':'Service 3'		}]	},{		'id':14,		'text':'Service table 3'	}]}]";            return JsonConvert.DeserializeObject
>(strJson);

不管三七二十一,将泛型对象定为 object 即可。

--------------------------------------------------

2019.06.28

转过去之后呢?可以强制转换为JObject进行使用

JObject obj = (JObject)JsonConvert.DeserializeObject(strdata);

 

转载地址:https://leftfist.blog.csdn.net/article/details/42435887 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Linq入门
下一篇:操作系统学习笔记三 进程

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月13日 09时24分56秒