Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This trick to support totally crazy mappings is to use <code>JsonConverter</code> and completely replace the parsing for that object, (I apologize for the C#, but I'm no good at VB syntax):</p> <pre><code>class Program { static void Main(string[] args) { var result = JsonConvert.DeserializeObject&lt;Responses&gt;(TestData); } const string TestData = @"{""result_ok"":true, ""total_count"":""44"", ""page"":1, ""total_pages"":1, ""results_per_page"":50, ""data"":[ {""id"":""1"", ""contact_id"":"""", ""status"":""Complete"", ""is_test_data"":""0"", ""datesubmitted"":""2011-11-13 22:26:53"", ""[question(59)]"":""11\/12\/2011"", ""[question(60)]"":""06:15 pm"", ""[question(62)]"":""72"", ""[question(63)]"":""One"", ""[question(69), option(10196)]"":""10"", }]}"; } [JsonObject] class Responses { public bool result_ok { get; set; } public string total_count { get; set; } public int page { get; set; } public int total_pages { get; set; } public int results_per_page { get; set; } public SurveyResponse[] Data { get; set; } } [JsonObject] // Here is the magic: When you see this type, use this class to read it. // If you want, you can also define the JsonConverter by adding it to // a JsonSerializer, and parsing with that. [JsonConverter(typeof(DataItemConverter))] class SurveyResponse { public string id { get; set; } public string contact_id { get; set; } public string status { get; set; } public string is_test_data { get; set; } public DateTime datesubmitted { get; set; } public Dictionary&lt;int, string&gt; questions { get; set; } } class DataItemConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(SurveyResponse); } public override bool CanRead { get { return true; } } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var value = (SurveyResponse)existingValue; if (value == null) { value = new SurveyResponse(); value.questions = new Dictionary&lt;int, string&gt;() } // Skip opening { reader.Read(); while (reader.TokenType == JsonToken.PropertyName) { var name = reader.Value.ToString(); reader.Read(); // Here is where you do your magic if (name.StartsWith("[question(")) { int index = int.Parse(name.Substring(10, name.IndexOf(')') - 10)); value.questions[index] = serializer.Deserialize&lt;string&gt;(reader); } else { var property = typeof(SurveyResponse).GetProperty(name); property.SetValue(value, serializer.Deserialize(reader, property.PropertyType), null); } // Skip the , or } if we are at the end reader.Read(); } return value; } public override bool CanWrite { get { return false; } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } </code></pre> <p>Now obviously there's a lot more you would want to do to get this really robust, but this gives you the basics of how to do it. There are more lightweight alternatives if you simply need to change property names (either <code>JsonPropertyAttribute</code> or overriding <code>DefaultContractResolver.ResolvePropertyName()</code>, but this gives you full control.</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. 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