51

If I have this method:

public void doSomething (Dictionary<String, Object> data)
{
    JObject jsonObject = new JObject(data);
    ...
}

I get a System.ArgumentException on the line where I create the JObject. I'm using Newton-King's Json.net wrapper.

The error I get is:

A first chance exception of type 'System.ArgumentException' occurred in Newtonsoft.Json.DLL An exception of type 'System.ArgumentException' occurred in Newtonsoft.Json.DLL but was not handled in user code

What am I doing wrong here?

7
  • is this NewtonSoft's JObject that you are using? Commented Aug 28, 2013 at 19:14
  • Posting your whole error is helpful as well.. is it Could not determine JSON object type for type System.Collections.Generic.KeyValuePair2[System.String,System.String].`? Commented Aug 28, 2013 at 19:26
  • @paqogomez The error message is A first chance exception of type 'System.ArgumentException' occurred in Newtonsoft.Json.DLL An exception of type 'System.ArgumentException' occurred in Newtonsoft.Json.DLL but was not handled in user code Commented Aug 28, 2013 at 19:30
  • 1
    Have you tried using JObject.Parse and passing in your json string? I'm not sure if that would be the string, or object portion of your method, but parse returns a jobject. Commented Aug 28, 2013 at 19:30
  • Is there anything in the InnerException? Commented Aug 28, 2013 at 19:32

1 Answer 1

112

The JObject(object) constructor is expecting the object to be either a JProperty, an IEnumerable containing JProperties, or another JObject. Unfortunately, the documentation does not make this clear.

To create a JObject from a dictionary or plain object, use JObject.FromObject instead:

JObject jsonObject = JObject.FromObject(data);

To create a JObject from a JSON string, use JObject.Parse, e.g.:

JObject jsonObject = JObject.Parse(@"{ ""foo"": ""bar"", ""baz"": ""quux"" }");
Sign up to request clarification or add additional context in comments.

2 Comments

how about creating a JObject from a string? How do you do that?
JObject.Parse(myJsonString)

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.