yerpy
yerpy

Reputation: 1446

Current JsonReader item is not an object

First I made an application and then I've started doing test for it ( Know it is not good way ), everything works fine with parsing etc, but after i made few test got an error :

Newtonsoft.Json.JsonReaderException : Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

Error occurs there jObject = JObject.Parse(content); and there arrayList = JArray.Parse(content);

internal JObject DoParse(string content)
{
    JObject jObject = new JObject();
    if (content != null)
    {
        if (content.Contains("Unable"))
        {
            MessageBox.Show("Not found.", "Error");
        }
        else
        {
            jObject = JObject.Parse(content);
        }
    }
    return jObject;
}

internal JArray DoParseOnList(string content)
{
    JArray arrayList = new JArray();
    if (content != null)
    {
        if (content.Contains("Unable"))
        {
            MessageBox.Show("Not found.", "Error");
        }
        else
        {

            arrayList = JArray.Parse(content);
        }
    }
    else { }
    return arrayList;
}

Any ideas what is wrong ? Btw. string content is json which i got from the server. Thanks in advance !

JSON

Test Name:  SetGroup
Test Outcome:   Failed
Result Message: SetUp : Newtonsoft.Json.JsonReaderException : Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Result StandardOutput:  [{"id":6208,"name":"test"},{"id":6315,"name":"jPOD v144 Testing"},{"id":6306,"name":"iButton Issue"},{"id":6424,"name":"Hybrid"}]
[{"id":6208,"name":"test"},{"id":6315,"name":"jPOD v144 Testing"},{"id":6306,"name":"iButton Issue"},{"id":6424,"name":"Hybrid"}]
[{"enabled":true,"scriptVersion":199,"configVersion":3,"name":"LMU3030 Hybrid Car Test based on 64.112 add ignition on-off"},{"enabled":true,"scriptVersion":199,"configVersion":2,"name":"LMU3030 Hybrid Car Test based on 50.106"},{"enabled":true,"scriptVersion":199,"configVersion":1,"name":"Hybrid car LMU 3030 Ignition test","description":""},{"enabled":true,"scriptVersion":64,"configVersion":113,"name":"based on 64.112 Engineering Build from calamp"},{"enabled":true,"scriptVersion":61,"configVersion":106},{"enabled":true,"scriptVersion":38,"configVersion":117},{"enabled":true,"scriptVersion":184,"configVersion":0},{"enabled":true,"scriptVersion":13,"configVersion":54},{"enabled":true,"scriptVersion":23,"configVersion":105,"name":"PULS Redirect to PROD","description":"Changes just Param 2320 to maint.vehicle-location.com"}]
[]
[{"message":"Not Implemented","vbusDeviceFiles":[],"vbusFileHistories":[]}]

Upvotes: 13

Views: 43894

Answers (4)

Seth
Seth

Reputation: 6832

Use JToken.Parse() instead of JObject.Parse() or JArray.Parse().

JToken is the base class for JObject and JArray. It's Parse() method works for both.

Upvotes: 0

mmacneill123
mmacneill123

Reputation: 33

@joedotnot is exactly right - look for [ or {. Interestingly WordPress API returns both depending on the context which makes parsing the JSON object or array of objects fun... JObject is also not a great choice for a variety of others reasons, any malformed JSON and it will bomb out, much better to deserialise into your type, a good guide is here which is applicable anywhere JSON.NET is used.

Upvotes: -1

Peter C
Peter C

Reputation: 201

I have similar issue.
The returned JSON is Array/List but not Object.
Instead, I use JArray.Parse and it works.

jArray = JArray.Parse(content);

Upvotes: 19

Stacy
Stacy

Reputation: 307

I ran into a very similar problem. The reason my JObject.Parse(json) would not work for me was because my Json had a beginning "[" and an ending "]". In order to make it work, I had to remove those two characters. I would inspect your Json and make sure that it starts with a { and ends with a }.

For me, I removed the first and last characters.

jsonResult = jsonResult.TrimStart(new char[] { '[' }).TrimEnd(new char[] { ']' });

Upvotes: 3

Related Questions