Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow">JavaScriptSerializer</a>? Nice integrated solution, allowed to be expanded upon with <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptconverter.aspx" rel="nofollow">JavaScriptConverter</a>s.</p> <hr> <p>Demo of what you were after:</p> <pre><code>using System; using System.Web.Script.Serialization; using System.Text; public class Stop { public String place { get; set; } public Int32 KMs { get; set; } } public class Trip { public String from { get; set; } public String to { get; set; } public Stop[] stops { get; set; } } public class MyJSONObject { public Trip[] Trips { get; set; } public override String ToString() { StringBuilder sb = new StringBuilder(); foreach (Trip trip in this.Trips) { sb.AppendFormat("Trip:\r\n"); sb.AppendFormat("\t To: {0}\r\n", trip.to); sb.AppendFormat("\t From: {0}\r\n", trip.from); sb.AppendFormat("\tStops:\r\n"); foreach (Stop stop in trip.stops) { sb.AppendFormat("\t\tPlace: {0}\r\n", stop.place); sb.AppendFormat("\t\t KMs: {0}\r\n", stop.KMs); } sb.AppendLine(); } return sb.ToString(); } } public class Test { public static void Main() { String example = "{\"Trips\":[{\"from\":\"here\",\"to\":\"there\",\"stops\":[{\"place\":\"middle1\",\"KMs\":37},{\"place\":\"middle2\",\"KMs\":54}]},{\"from\":\"there\",\"to\":\"here\",\"stops\":[{\"place\":\"middle2\",\"KMs\":37},{\"place\":\"middle1\",\"KMs\":54}]}]}"; JavaScriptSerializer serializer = new JavaScriptSerializer(); // Parse the string to our custom object MyJSONObject result = serializer.Deserialize&lt;MyJSONObject&gt;(example); Console.WriteLine("Object deserialized:\r\n\r\n{0}\r\n\r\n", result); // take our custom object and dump it in to a string StringBuilder sb = new StringBuilder(); serializer.Serialize(result, sb); Console.WriteLine("Object re-serialized:\r\n\r\n{0}\r\n\r\n", sb); // check step Console.WriteLine(example.Equals(sb.ToString()) ? "MATCH" : "NO match"); } } </code></pre> <p>And the output:</p> <pre><code>Object deserialized: Trip: To: there From: here Stops: Place: middle1 KMs: 37 Place: middle2 KMs: 54 Trip: To: here From: there Stops: Place: middle2 KMs: 37 Place: middle1 KMs: 54 Object re-serialized: {"Trips":[{"from":"here","to":"there","stops":[{"place":"middle1","KMs":37},{"pl ace":"middle2","KMs":54}]},{"from":"there","to":"here","stops":[{"place":"middle 2","KMs":37},{"place":"middle1","KMs":54}]}]} MATCH Press any key to continue . . . </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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