0

I'm using JSON.net in C# for an Excel VSTO Add in and pulling in JSON via web service.

I have verified the JSON I pull is valid (online JSON Validator) but am unable to convert the JSON into Objects in C# to use.

When I run the code below I get the exception below.

Any ideas on who I can covert the JSON correctly?

Exception:

Newtonsoft.Json.JsonSerializationException: 

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Bliss.Ribbon1+RootObject[]' 
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type 
(e.g. not a primitive type like integer, not a collection type like an array or List<T>) 
that can be deserialized from a JSON object. 
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Code:

public async Task<string> getline()
{
  <--- Set Client, Execute Request --->

  //JSON content shown below
  string content = await response.Content.ReadAsStringAsync();

  RootObject[] dims = JsonConvert.DeserializeObject<RootObject[]>(content);

  return content;
}

public class RootObject
{
  public List<string> ledger { get; set; }
  public List<string> ledgerAlias { get; set; }
  public List<string> entity { get; set; }
  public List<string> entityAlias { get; set; }
  public List<string> scenario { get; set; }
  public List<string> period { get; set; }
  public List<string> location { get; set; }
  public List<string> measures { get; set; }
}

JSON:

{
 "acc":["A","B"],
 "accAlias":["ACE","BCE"],
 "company":["PEP", "KO"],
 "companyAlias":["Pepsi", "Coco-Cola"],
 "scenario":["Sales", "Expenses"],
 "year": ["2016","2017"],
 "areaCode":["123","131","412"],
 "clients":["32340-0120","3031-0211","3412-0142"]
}
4
  • 3
    You are trying to deserialize an array of RootObjects, but it is only one object. Change RootObject[] to RootObject Commented Sep 21, 2016 at 16:45
  • It sounds like you're asking what the word "array" means. But the error message actually told you what it means. Commented Sep 21, 2016 at 16:45
  • 2
    Additionally your object definition does not match your JSON Commented Sep 21, 2016 at 16:49
  • 2
    At the risk of stating the obvious, none of the property names in RootObject match the field names in your JSON except for scenario. That isn't your problem, @David already answered that, but even once you get it working you'll find most of your properties are null. Commented Sep 21, 2016 at 16:49

2 Answers 2

2

The JSON represents a single instance of the object, not an array. So instead of this:

RootObject[] dims = JsonConvert.DeserializeObject<RootObject[]>(content)

use this:

RootObject dims = JsonConvert.DeserializeObject<RootObject>(content)

Conversely, if it should be an array, make the JSON itself an array (containing a single element) by surrounding it with brackets:

[{
 "acc":["A","B"],
 "accAlias":["ACE","BCE"],
 "company":["PEP", "KO"],
 "companyAlias":["Pepsi", "Coco-Cola"],
 "scenario":["Sales", "Expenses"],
 "year": ["2016","2017"],
 "areaCode":["123","131","412"],
 "clients":["32340-0120","3031-0211","3412-0142"]
}]

Edit: As others have also been pointing out, the properties on the JSON object don't actually match that class definition. So while it may "successfully" deserialize, in doing so it's going to ignore the JSON properties it doesn't care about and initialize to default values the class properties.

Perhaps you meant to use a different class? Or different JSON? Or rename one or more properties in one or the other?

Either way, the difference between a single instance and an array of instances is the immediate problem. But in correcting that problem you're going to move on to this next one.

Sign up to request clarification or add additional context in comments.

Comments

0

The RootObject and the json are not compatible. You could deserialize using a dictionary. Try this:

var dims = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(content);

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.