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

如何使用Newtonsoft.Json包在C#(4.0)中解析我的json字符串?

时间:2023-04-11 00:45:05 C#

如何使用Newtonsoft.Json包在C#(4.0)中解析我的json字符串?我是JSON的新手。在我的asp.net应用程序中,我想解析json字符串。所以,我使用Newtonsoft.Json包来读写json数据。现在,我能够解析简单的json数据。但现在我收到一些复杂的json数据进行解析。所以,我有点把它搞砸了。这是JSON数据:{quizlist:[{QUIZ:{'QPROP':[{'name':'FB','intro':'','timeopen':'1347871440','timeclose':'1355733840','timelimit':'0','noofques':'5','QUESTION':{'QUEPROP':[{'questiontext':'Scienceisbasedont','penalty':'0.3333333','qtype':'shortanswer','answer':'cause-and-effect','mark':'5','hint':''},{'questiontext':'otherscientistsevaluateit','penalty':'0.3333333','qtype':'shortanswer','answer':'Peerreview','mark':'5','hint':''},{'questiontext':'Watchingavariety','penalty':'0.3333333','qtype':'shortanswer','answer':'inductive','mark':'5','hint':''},{'questiontext':'coveriesorideas','penalty':'0.3333333','qtype':'shortanswer','answer':'paradigmshift','mark':'5','hint':''},{'questiontext':'proportions','penalty':'0.3333333','qtype':'shortanswer','answer':'fixed','mark':'5','hint':''}]}}]}}]}这是我的C#代码:dynamicdynObj=JsonConvert.DeserializeObject(jsonString);foreach(vardataindynObj.quizlist){foreach(vardata1indata.QUIZ.QPROP){Response.Write("Name"+":"+data1.name+"");Response.Write("Intro"+":"+data1.intro+"");Response.Write("Timeopen"+":"+data1.timeopen+"");Response.Write("Timeclose"+":"+data1.timeclose+"");Response.Write("时间限制"+":"+data1.timelimit+"");Response.Write("Noofques"+":"+data1.noofques+"");}}i能够解析QPROP数组对象中的最多noofques个对象现在必须解析data.QUIZ.QPROP.QUESTION.QUEPROP数组对象......但我没有完全解析......请指导我解决这个问题......foreach(dynObj.quizlist中的var数据){foreach(data.QUIZ.QPROP中的vardata1){Response.Write(“名称”+“:”+data1.name+“”);Response.Write("简介"+":"+data1.intro+"");Response.Write("Timeopen"+":"+data1.timeopen+"");Response.Write("Timeclose"+":"+data1.timeclose+"");Response.Write("时间限制"+":"+data1.timelimit+"");Response.Write("Noofques"+":"+data1.noofques+"");foreach(varquepropindata1.QUESTION.QUEPROP){Response.Write("Questiontext"+":"+queprop.questiontext+"");Response.Write("Mark"+":"+queprop.mark+"");您可以使用此工具创建适当的c#类:http://jsonclassgenerator.codeplex.com/当您创建类时,您可以简单地将字符串转换为对象:publicstaticTParseJsonObject(stringjson)其中T:class,new(){JObjectjobject=JObject.Parse(json);返回JsonConvert.DeserializeObject(jobject.ToString());这些类:http://ge.tt/2KGtbPT/v/0?c只需修复命名空间问候您可以创建自己的测验类型类,然后使用强类型反序列化:示例:quizresult=JsonConvert.DeserializeObject(args.Message,newJsonSerializerSettings{Error=delegate(objectsender1,ErrorEventArgsargs1){errors.Add(args1.ErrorContext.Error.Message);args1.ErrorContext.Handled=true;}});您还可以应用架构验证。http://james.newtonking.com/projects/json/help/index.html这是一个JSON解析的简单例子,以googlemapAPI为例。这将返回给定邮政编码的城市名称。以上是C#学习教程:HowtousetheNewtonsoft.JsonpackagetoparsemyjsonstringinC#(4.0)?所有分享的内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——usingSystem;使用System.Collections.Generic;使用System.Linq;使用System.Web;使用System.Web使用System.Web.UI.WebControls;使用Newtonsoft.Json;使用System.Net;namespaceWebApplication1{publicpartialclassWebForm1:System.Web.UI.Page{WebClientclient=newWebClient();字符串jsonstring;protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse){jsonstring=client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address="+txtzip.Text.Trim());动态dynObj=JsonConvert.DeserializeObject(jsonstring);Response.Write(dynObj.results[0].address_components[1].long_name);}}}本文采集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: