Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I wrote a ModelBinder that does exactly what you wanted:</p> <pre><code>public class DictionaryModelBinder : DefaultModelBinder { private const string _dateTimeFormat = "dd/MM/yyyy HH:mm:ss"; private enum StateMachine { NewSection, Key, Delimiter, Value, ValueArray } public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var stream = controllerContext.HttpContext.Request.InputStream; string text; stream.Position = 0; using (var reader = new StreamReader(stream)) { text = reader.ReadToEnd(); } int index = 0; return Build(text, ref index); } private static Dictionary&lt;string, object&gt; Build(string text, ref int index) { var state = StateMachine.NewSection; var dictionary = new Dictionary&lt;string, object&gt;(); var key = string.Empty; object value = string.Empty; for (; index &lt; text.Length; ++index) { if (state == StateMachine.NewSection &amp;&amp; text[index] == '{') { dictionary = new Dictionary&lt;string, object&gt;(); state = StateMachine.NewSection; } else if (state == StateMachine.NewSection &amp;&amp; text[index] == '"') { key = string.Empty; state = StateMachine.Key; } else if (state == StateMachine.Key &amp;&amp; text[index] != '"') { key += text[index]; } else if (state == StateMachine.Key &amp;&amp; text[index] == '"') { state = StateMachine.Delimiter; } else if (state == StateMachine.Delimiter &amp;&amp; text[index] == ':') { state = StateMachine.Value; value = string.Empty; } else if (state == StateMachine.Value &amp;&amp; text[index] == '[') { state = StateMachine.ValueArray; value = value.ToString() + text[index]; } else if (state == StateMachine.ValueArray &amp;&amp; text[index] == ']') { state = StateMachine.Value; value = value.ToString() + text[index]; } else if (state == StateMachine.Value &amp;&amp; text[index] == '{') { value = Build(text, ref index); } else if (state == StateMachine.Value &amp;&amp; text[index] == ',') { dictionary.Add(key, ConvertValue(value)); state = StateMachine.NewSection; } else if (state == StateMachine.Value &amp;&amp; text[index] == '}') { dictionary.Add(key, ConvertValue(value)); return dictionary; } else if (state == StateMachine.Value || state == StateMachine.ValueArray) { value = value.ToString() + text[index]; } } return dictionary; } private static object ConvertValue(object value) { string valueStr; if (value is Dictionary&lt;string, object&gt; || value == null || (valueStr = value.ToString()).Length == 0) { return value; } bool boolValue; if (bool.TryParse(valueStr, out boolValue)) { return boolValue; } int intValue; if (int.TryParse(valueStr, out intValue)) { return intValue; } double doubleValue; if (double.TryParse(valueStr, out doubleValue)) { return doubleValue; } valueStr = valueStr.Trim('"'); DateTime datetimeValue; if (DateTime.TryParseExact(valueStr, _dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out datetimeValue)) { return datetimeValue; } if (valueStr.First() == '[' &amp;&amp; valueStr.Last() == ']') { valueStr = valueStr.Trim('[', ']'); if (valueStr.Length &gt; 0) { if (valueStr[0] == '"') { return valueStr .Split(new[] { '"' }, StringSplitOptions.RemoveEmptyEntries) .Where(x =&gt; x != ",") .ToArray(); } else { return valueStr .Split(',') .Select(x =&gt; ConvertValue(x.Trim())) .ToArray(); } } } return valueStr; } } </code></pre> <p>More explanations and full post you can see in my blog:</p> <p><a href="http://amiryonatan.blogspot.co.il/2013/01/json-to-dictionary-generic-model-binder.html" rel="nofollow">Json To Dictionary generic model binder</a></p>
 

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