5

I am trying to deserialize a JSON response I get from a webservice. I am trying to use NewtonSoft Json.NET.

I am trying this to parse the response

var results = JArray.Parse(response.Content);

I get following exception

Newtonsoft.Json.JsonReaderException occurred HResult=0x80131500
Message=Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.
Source=Newtonsoft.Json

I probably need to define the object to return but am not sure how to specify following response (sorry about the formatting, the indentions was removed by the editor here):

{"result": [
      {
      "recordType": "sys_ui_script",
      "hits": [],
      "tableLabel": "UI Script"
   },
      {
      "recordType": "sys_script",
      "hits":       [
                  {
            "name": "Approval Events (Non-Task)",
            "className": "sys_script",
            "tableLabel": "sys_script",
            "matches": [            {
               "field": "script",
               "fieldLabel": "Script",
               "lineMatches":                [
                                    {
                     "line": 21,
                     "context": "         updateRecord(current, current.approver.getDisplayValue() + \" rejected the task.\", ",
                     "escaped": "         updateRecord(current, current.approver.getDisplayValue() + " rejected the task.", "
                  }
               ],
               "count": 2
            }],
            "sysId": "ad15c8149f4010008f88ed93ee4bcc9f",
            "modified": 1489179469000
         }
      ],
      "tableLabel": "Business Rule"
   }

]}
4
  • Does you JSON have a method call in it? Whats the final output you get? This isn't valid. Commented Sep 20, 2017 at 15:44
  • 1
    {"result": [ is an object, not an array (and the error says so! "item is not an array"). Did you try JObject.Parse(...)? Commented Sep 20, 2017 at 15:46
  • Above is the output I get which I cannot change as it is an existing rest service on the net. Commented Sep 20, 2017 at 15:46
  • crashmstr2, thank you that solved my problem. Commented Sep 20, 2017 at 15:48

2 Answers 2

5

Define a class and deserialize it:

var results =  JsonConvert.DeserializeObject<RootObject>(response.Content);   

public class LineMatch
{
    public int line { get; set; }
    public string context { get; set; }
    public string escaped { get; set; }
}

public class Match
{
    public string field { get; set; }
    public string fieldLabel { get; set; }
    public List<LineMatch> lineMatches { get; set; }
    public int count { get; set; }
}

public class Hit
{
    public string name { get; set; }
    public string className { get; set; }
    public string tableLabel { get; set; }
    public List<Match> matches { get; set; }
    public string sysId { get; set; }
    public long modified { get; set; }
}

public class Result
{
    public string recordType { get; set; }
    public List<Hit> hits { get; set; }
    public string tableLabel { get; set; }
}

public class RootObject
{
    public List<Result> result { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you KreepN, Much appreciated.
You can also generate this code from the sample data with csharp.quicktype.io
4

As you are parsing an json object you should use

var results = JObject.Parse(response.Content);

JArray.Parse is for arrays as

 ['Small', { 'oneProp': 'Medium' }, 'Large' ]

You can see the documentation here.

Comments

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.