Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse my json string in C#(4.0)using Newtonsoft.Json package?

I am new to JSON.In my asp.net application i want to parse the json string.So, i have used Newtonsoft.Json package for reading and writing json data.Now, i can able to parse the simple json data.But now i have received some complex json data for parsing.So, i little bit struck on it.

This is JSON Data:

{     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': ''                                 }                             ]                         }                     }                 ]             }         }      ] } 

This is my C# Code :

dynamic dynObj = JsonConvert.DeserializeObject(jsonString);              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>");                 }               } 

I can able to parse until noofques object in QPROP array objects.Now have to parse data.QUIZ.QPROP.QUESTION.QUEPROP array objects also...

But i failed to parse fully...

Please guide me to get out of this issue...

like image 277
Saravanan Avatar asked Dec 12 '12 12:12

Saravanan


People also ask

Can we convert string to JSON in C?

Convert String to JSON Object With the JObject. Parse() Function in C# The JObject class inside the Newtonsoft. Json package is used to represent a JSON object in C#.

What is JSON parsing example?

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it. Android provides four different classes to manipulate JSON data.

What is JSMN?

jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You can find more information about JSON format at json.org. Library sources are available at https://github.com/zserge/jsmn.


1 Answers

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>");         }     } } 
like image 56
L.B Avatar answered Oct 08 '22 15:10

L.B