如何使用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#代码:

 dynamic dynObj = JsonConvert.DeserializeObject(jsonString); foreach (var data in dynObj.quizlist) { foreach (var data1 in data.QUIZ.QPROP) { Response.Write("Name" + ":" + data1.name + "
"); Response.Write("Intro" + ":" + data1.intro + "
"); Response.Write("Timeopen" + ":" + data1.timeopen + "
"); Response.Write("Timeclose" + ":" + data1.timeclose + "
"); Response.Write("Timelimit" + ":" + data1.timelimit + "
"); Response.Write("Noofques" + ":" + data1.noofques + "
"); } }

我能够解析直到QPROP数组对象中的noofques对象。现在必须解析data.QUIZ.QPROP.QUESTION.QUEPROP数组对象…

但我没有完全解析……

请指导我摆脱这个问题……

 foreach (var data in dynObj.quizlist) { foreach (var data1 in data.QUIZ.QPROP) { Response.Write("Name" + ":" + data1.name + "
"); Response.Write("Intro" + ":" + data1.intro + "
"); Response.Write("Timeopen" + ":" + data1.timeopen + "
"); Response.Write("Timeclose" + ":" + data1.timeclose + "
"); Response.Write("Timelimit" + ":" + data1.timelimit + "
"); Response.Write("Noofques" + ":" + data1.noofques + "
"); foreach (var queprop in data1.QUESTION.QUEPROP) { Response.Write("Questiontext" + ":" + queprop.questiontext + "
"); Response.Write("Mark" + ":" + queprop.mark + "
"); } } }

您可以使用此工具创建适当的c#类:

http://jsonclassgenerator.codeplex.com/

当你创建类时,你可以简单地将字符串转换为对象:

  public static T ParseJsonObject(string json) where T : class, new() { JObject jobject = JObject.Parse(json); return JsonConvert.DeserializeObject(jobject.ToString()); } 

这些类: http : //ge.tt/2KGtbPT/v/0?c

只需修复名称空间。

问候

您可以创建自己的Quiz类型类,然后使用强类型反序列化:

例:

 quizresult = JsonConvert.DeserializeObject(args.Message, new JsonSerializerSettings { Error = delegate(object sender1, ErrorEventArgs args1) { errors.Add(args1.ErrorContext.Error.Message); args1.ErrorContext.Handled = true; } }); 

您还可以应用架构validation。

http://james.newtonking.com/projects/json/help/index.html

这是一个简单的JSON解析示例,以google map API为例。 这将返回给定邮政编码的城市名称。

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Newtonsoft.Json; using System.Net; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { WebClient client = new WebClient(); string jsonstring; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { jsonstring = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address="+txtzip.Text.Trim()); dynamic dynObj = JsonConvert.DeserializeObject(jsonstring); Response.Write(dynObj.results[0].address_components[1].long_name); } } }