It's difficult to give a better answer without the information requested in comments, but this means, as it suggests, that the current token isn't the start of an object.
Just to be clear, we're talking about "object" in the JSON vocabulary, and not in the C#/OOP vocabulary sense.
It sounds like what's going on here is something like the non-JSON bool.Parse("1")
. Yes, "1"
is valid input to a parse method (int.Parse
, for instance), but this is the wrong method.
The simple trick (warning: Band-Aid fix) is to switch to JToken.Parse
. JToken
is a polymorphic parent of JObject
, JArray
, JValue
, and the lot, so its parse is able to handle a lot more types of unstructured input.
Before you do that, as I'm sure you know, you should of course double-check your contracts and the file, to see which is wrong. There's certainly no good in successfully parsing a JArray
that you then use as a JObject
.
For good measure, a few file texts that would cause this error are:
(empty file)
[{ "test": "val" }]
(array)
"test"
(string)
null
These should all be valid inputs to JToken.Parse
, I believe, but they'd all give this error on JObject.Parse
, because they aren't objects. You need something like:
{ "test": "val" }
{ "test": { "val": 2 } }
Or something to that effect.
Back on my OOP point, a JObject
isn't the elementary base type of everything in JSON.net, but JToken
is. So even though you could say,
object i = new int[0];
in C#, you can't say,
JObject i = JObject.Parse("[0, 0, 0]");
in JSON.net.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…