当前位置: 首页 > 编程语言 > C#

在C#中反序列化JSON数组(或列表)分享

时间:2023-04-11 12:06:31 C#

在C#中反序列化JSON数组(或列表)这是基础代码:last":"Smith"},{"last":"Doe"}]}";JavaScriptSerializerser=newJavaScriptSerializer();nameListmyNames=ser.Deserialize(jsonData);返回ser.Serialize(myNames);}//类描述publicclassname{publicstringlast{get;放;}}publicclassnameList{publicList{get;放;我试过了:nameListmyNames=ser.Deserialize(jsonData);和:ListmyNames=ser.Deserialize(jsonData);并且都不起作用。有人有想法吗?这段代码对我有用:使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;使用System.Web.Script.Serialization;namespaceJson{classProgram{staticvoidMain(string[]args){Console.WriteLine(DeserializeNames());控制台.ReadLine();}publicstaticstringDeserializeNames(){varjsonData="{"name":[{"last":"Smith"},{"last":"Doe"}]}";JavaScriptSerializerser=newJavaScriptSerializer();nameListmyNames=ser.Deserialize(jsonData);返回ser.Serialize(myNames);}//类描述publicclassname{publicstringlast{get;放;}}publicclassnameList{publicListname{get;放;从这里下载Json.NEThttp://james.newtonking.com/projects/json-net.aspxnamedeserializedName=JsonConvert.DeserializeObject(jsonData);我有一个类似的问题并通过理解asp.netC#中的类解决了它我想阅读以下JSON字符串:[{"resultList":[{"channelType":"","duration":"2:29:30","episodeno":0,"genre":"戏剧","genreList":["戏剧"],"流派”:[{“personName”:“戏剧”}],“id”:1204,“语言”:“印地语”,“名称”:“伟大的目标”,“productId”:1204,“productMasterId”:1203,"productMasterName":"TheGreatTarget","productName":"TheGreatTarget","productTypeId":1,"productTypeName":"Movie","rating":3,"releaseyear":"2005","showGoodName":"Movies","views":8333},{"channelType":"","duration":"2:30:30","episodeno":0,"genre":"Romance","genreList":["Romance"],"genres":[{"personName":"Romance"}],"id":1144,"language":"Hindi","name":"MereSapnonKiRani","productId":1144,"productMasterId":1143,"productMasterName":"MereSapnonKiRani","productName":"MereSapnonKiRani","productTypeId":1,"productTypeName":"Movie","rating":3,"releaseyear":"1997","showGoodName":"Movies","views":6482},{"channelType":"","duration":"2:34:07","episodeno":0,“流派”:“戏剧”,“流派列表”:[“戏剧”],“流派”:[{“人名”:“戏剧”}],"id":1520,"language":"Telugu","name":"SatyamevaJayathe","productId":1520,"productMasterId":1519,"productMasterName":"SatyamevaJayathe","productName":"SatyamevaJayathe","productTypeId":1,"productTypeName":"Movie","rating":3,"releaseyear":"2004","showGoodName":"Movies","views":9910}],"resultSize":1171,"pageIndex":"1"}]我的asp.netc#代码如下所示首先,在Web应用程序的APP_Code文件夹中创建Class3.cs页面usingSystem;使用系统数据;使用系统配置;使用System.Web;使用System.Web.Security;使用System.Web.UI;使用System.Web.UI.WebControls;使用System.Web.UI.WebControls.WebParts;使用System.Web.UI.HtmlControls;使用系统集合;使用系统文本;使用System.IO;使用System.Web.Script.Serialization;使用System.Collections.Generic;//////Class3的摘要描述///publicclassClass3{publicListResultList_Main{get;放;}publicclassListWrapper_Main{publicListResultList{get;放;}publicstringresultSize{得到;放;}publicstringpageIndex{get;放;}}publicclassListWrapper{publicstringchannelType{get;放;}公共字符串持续时间{get;放;}publicintepisodeno{get;放;}publicstringgenre{get;放;}publicstring[]genreList{get;放;}publicListgenres{get;放;}publicintid{得到;放;}publicstringimageUrl{get;放;}//publicstringimageurl{get;放;}公共字符串语言{get;放;}公共字符串名称{get;放;}publicintproductId{得到;放;}publicintproductMasterId{得到;放;}publicstringproductMasterName{得到;放;}publicstringproductName{得到;放;}publicintproductTypeId{得到;放;}publicstringproductTypeName{得到;放;}publicdecimalrating{get;放;}publicstringreleaseYear{get;放;}//publicstringreleaseyear{get;放;}publicstringshowGoodName{get;放;}公共字符串视图{获取;放;}}公开课genres_cls{publicstringpersonName{get;放;然后浏览器页面读取上面列出的字符串/JSON字符串并显示/反序列化JSON对象并显示数据JavaScriptSerializerser=newJavaScriptSerializer();字符串final_sb=sb.ToString();列出movieInfos=ser.Deserialize>(final_sb.ToString());foreach(varitemdetailinmovieInfos){foreach(varitemdetail2initemdetail.ResultList){Response.Write("channelType="+itemdetail2.channelType+"");Response.Write("duration="+itemdetail2.duration+"");Response.Write("episodeno="+itemdetail2.episodeno+"");Response.Write("genre="+itemdetail2.genre+"");string[]genreList_arr=itemdetail2.genreList;for(inti=0;i'sb'是实际的字符串,上面首先提到的JSON数据字符串回复thisBasically-webapplicationasp.netc#code....Nhappy...以上是C#学习教程:DeserializingJSONArray(orList)inC#分享全部内容,如果对大家有用并且需要了解更多C#学习教程,希望大家多多关注~本文收集自网络,确实不代表立场,如涉及侵权,请点击右边联系管理员删除,如需转载请注明出处: