title: How to convert dynamically in C# the parsed JSON result from Json.NET?
body: <p>I have implemented a JSON-RPC with Json.NET. The result of the response I convert with:</p>
  
  <pre><code>T data = ((JToken)value).ToObject&lt;T&gt;();
  </code></pre>
  
  <p>This work if the result is an object. But it does not work if the result is an primitive like Int64 or an string. Then the value is not a JToken.</p>
  
  <p>I have found the method:</p>
  
  <pre><code>static object Convert(object initialValue, CultureInfo culture, Type targetType)
  </code></pre>
  
  <p>which seems to do the needed job. But the class ConvertUtils is internal. It there any public method to convert any parsed JSON result to any target type?</p>
  
  <p>A more completely sample with the JSON-RPC handling:</p>
  
  <pre><code>JsonRpcResult result = serializer.Deserialize&lt;JsonRpcResult&gt;(jsonTextReader);
  Type type = requests[result.id];
  object value = result.result;
  return ((JToken)value).ToObject(type);
  </code></pre>
  
  <p>A typical JSON string:</p>
  
  <pre><code>{"jsonrpc":"2.0","result":DynamicValidJsonDependsOnID,"id":1}
  </code></pre>
  
  <p>The JSON content of DynamicValidJsonDependsOnID depends on the id and can be any valid JSON. There are no limits.</p>
  
  <p>The class JsonRpcResult:</p>
  
  <pre><code>[JsonObject(MemberSerialization.Fields)]
  internal class JsonRpcResult
  {
      private string jsonrpc;
      private object result;
      private Dictionary&lt;String,Object&gt; error;
      private object id;
   }
  </code></pre>