Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had the same problem, so I wrote this my self. This solution is differentiated from other answers because it can deserialize in to multiple levels. </p> <p>Just send JSON string in to <strong><em>deserializeToDictionary</em></strong> function it will return non strongly-typed <em><code>Dictionary&lt;string, object&gt;</code></em> object.</p> <p><strong>Old code</strong></p> <pre><code>private Dictionary&lt;string, object&gt; deserializeToDictionary(string jo) { var values = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(jo); var values2 = new Dictionary&lt;string, object&gt;(); foreach (KeyValuePair&lt;string, object&gt; d in values) { // if (d.Value.GetType().FullName.Contains("Newtonsoft.Json.Linq.JObject")) if (d.Value is JObject) { values2.Add(d.Key, deserializeToDictionary(d.Value.ToString())); } else { values2.Add(d.Key, d.Value); } } return values2; } </code></pre> <p>Ex: This will return <code>Dictionary&lt;string, object&gt;</code> object of a Facebook JSON response.</p> <p><strong>Test</strong></p> <pre><code>private void button1_Click(object sender, EventArgs e) { string responsestring = "{\"id\":\"721055828\",\"name\":\"Dasun Sameera Weerasinghe\",\"first_name\":\"Dasun\",\"middle_name\":\"Sameera\",\"last_name\":\"Weerasinghe\",\"username\":\"dasun\",\"gender\":\"male\",\"locale\":\"en_US\", hometown: {id: \"108388329191258\", name: \"Moratuwa, Sri Lanka\",}}"; Dictionary&lt;string, object&gt; values = deserializeToDictionary(responsestring); } </code></pre> <blockquote> <p>Note: hometown further deserilize into a <code>Dictionary&lt;string, object&gt;</code> object.</p> </blockquote> <p><strong>Update</strong></p> <p>My old answer works great if there is no array on JSON string. This one further deserialize in to a <code>List&lt;object&gt;</code> if an element is an array.</p> <p>Just send a JSON string in to <strong><em>deserializeToDictionaryOrList</em></strong> function it will return non strongly-typed <em><code>Dictionary&lt;string, object&gt;</code></em> object or <em><code>List&lt;object&gt;</code></em>.</p> <pre><code>private static object deserializeToDictionaryOrList(string jo,bool isArray=false) { if (!isArray) { isArray = jo.Substring(0, 1) == "["; } if (!isArray) { var values = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(jo); var values2 = new Dictionary&lt;string, object&gt;(); foreach (KeyValuePair&lt;string, object&gt; d in values) { if (d.Value is JObject) { values2.Add(d.Key, deserializeToDictionary(d.Value.ToString())); } else if (d.Value is JArray) { values2.Add(d.Key, deserializeToDictionary(d.Value.ToString(), true)); } else { values2.Add(d.Key, d.Value); } } return values2; }else { var values = JsonConvert.DeserializeObject&lt;List&lt;object&gt;&gt;(jo); var values2 = new List&lt;object&gt;(); foreach (var d in values) { if (d is JObject) { values2.Add(deserializeToDictionary(d.ToString())); } else if (d is JArray) { values2.Add(deserializeToDictionary(d.ToString(), true)); } else { values2.Add(d); } } return values2; } } </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. 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.
    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