Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I prefer to use at least frameworks as possible. Look at my code.</p> <p>For the given object structure:</p> <pre><code>public class QuestionsRepository { public List&lt;QuestionObj&gt; Questions; } public class QuestionObj { public string Question; public UInt16 CorrectAnswer; public AnswerObj[] Answers; } public class AnswerObj { public string Answer; } </code></pre> <p>Declare trivial simplest wrapper:</p> <pre><code>public static class JsonUtils { class JsonSerializer&lt;T&gt; { static DataContractJsonSerializer xs = new DataContractJsonSerializer(typeof(T)); public static object DeserializeObject(string serializedData, Encoding encoding) { byte[] data = encoding.GetBytes(serializedData); MemoryStream sr = new MemoryStream(data); return xs.ReadObject(sr); } public static string SerializeObject(T obj, Encoding encoding) { MemoryStream ms = new MemoryStream(); xs.WriteObject(ms, obj); byte[] data = ms.ToArray(); return encoding.GetString(data); } } public static T DeserializeObject&lt;T&gt;(this string serializedData) { return (T)JsonSerializer&lt;T&gt;.DeserializeObject(serializedData, Encoding.Default); } public static string SerializeObject&lt;T&gt;(this T obj) { return JsonSerializer&lt;T&gt;.SerializeObject(obj, Encoding.Default); } } </code></pre> <p>Sample:</p> <pre><code>class Program { static void Main() { try { string json = "{\"Questions\": [{ \"Question\": \"Who was the Chola King who brought Ganga from North to South?\", \"CorrectAnswer\": 1, \"Answers\": [ { \"Answer\": \"Raja Raja Chola\" }, { \"Answer\": \"Rajendra Chola\" }, { \"Answer\": \"Parantaka\" }, { \"Answer\": \"Mahendra\" } ] }, { \"Question\": \"The writ of 'Habeas Corpus' is issued in the event of:\", \"CorrectAnswer\": 2 , \"Answers\": [{ \"Answer\": \"Loss of Property\" }, { \"Answer\": \"Refund of Excess Taxes\" }, { \"Answer\": \"Wrongful Police Detention\" }, { \"Answer\": \"Violation of the Freedom of Speech\" }] }]}}"; QuestionsRepository newRepository = json.DeserializeObject&lt;QuestionsRepository&gt;(); for (int i = 0; i &lt; newRepository.Questions.Count; i++) { Console.WriteLine("{0}", newRepository.Questions[i].Question); int count = 1; foreach (var answer in newRepository.Questions[i].Answers) { Console.WriteLine("\t{0}) {1} ({2})", count, answer.Answer, newRepository.Questions[i].CorrectAnswer == count ? "+" : "-"); count++; } } } catch (SerializationException serEx) { Console.WriteLine(serEx.Message); Console.WriteLine(serEx.StackTrace); } } } </code></pre> <p>P.S.: Classes must be top-level enities with default constructor available (visible, accessible classes for data serializer) to be uses in <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.90%29.aspx" rel="nofollow">DataContractJsonSerializer</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload