2

I have a big JSON string I receive from a third-party library (so, I'm unable to change its format). I deserialize it using JsonConvert.DeserializeObject. Then, two properties of this newly created object are: JObject and its corresponding System.Type. So, I need to implement the method below to parse it to System.Object of known at runtime System.Type.

object ConvertJObjectToObject(Type t, JObject o)
{
    return ........
}

JObject.ToObject is a generic method and works only with compile-type types. JsonConvert.DeserializeObject requires JSON string as its first argument, but I have parsed JObject on this stage, and do not want to call ToString() on JObject.

Is there any elegant way to accomplish this?

1 Answer 1

4

There is an overload that takes Type as parameter:

return o.ToObject(t);
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure why, but my JSON.NET does not have such method.
Well, okay. I figured out the reason. 4.5 version of JSON.NET does not have this feature, current version 6.0 has. Thx, valid answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.