Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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":[]}]
like image 801
yerpy Avatar asked Nov 24 '16 15:11

yerpy


2 Answers

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);
like image 151
Peter C Avatar answered Nov 15 '22 23:11

Peter C


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[] { ']' });
like image 23
Stacy Avatar answered Nov 15 '22 22:11

Stacy