How to parse my json string in C#(4.0)using Newtonsoft.Json package?
foreach (var data in dynObj.quizlist)
{
foreach (var data1 in data.QUIZ.QPROP)
{
Response.Write("Name" + ":" + data1.name + "<br>");
Response.Write("Intro" + ":" + data1.intro + "<br>");
Response.Write("Timeopen" + ":" + data1.timeopen + "<br>");
Response.Write("Timeclose" + ":" + data1.timeclose + "<br>");
Response.Write("Timelimit" + ":" + data1.timelimit + "<br>");
Response.Write("Noofques" + ":" + data1.noofques + "<br>");
foreach (var queprop in data1.QUESTION.QUEPROP)
{
Response.Write("Questiontext" + ":" + queprop.questiontext + "<br>");
Response.Write("Mark" + ":" + queprop.mark + "<br>");
}
}
}
You can use this tool to create appropriate c# classes:
http://jsonclassgenerator.codeplex.com/
and when you will have classes created you can simply convert string to object:
public static T ParseJsonObject<T>(string json) where T : class, new()
{
JObject jobject = JObject.Parse(json);
return JsonConvert.DeserializeObject<T>(jobject.ToString());
}
Here that classes: http://ge.tt/2KGtbPT/v/0?c
Just fix namespaces.
You could create your own class of type Quiz and then deserialize with strong type:
Example:
quizresult = JsonConvert.DeserializeObject<Quiz>(args.Message,
new JsonSerializerSettings
{
Error = delegate(object sender1, ErrorEventArgs args1)
{
errors.Add(args1.ErrorContext.Error.Message);
args1.ErrorContext.Handled = true;
}
});
And you could also apply a schema validation.
http://james.newtonking.com/projects/json/help/index.html