简体   繁体   English

C#的.NET Json序列化异常

[英].NET Json Serialization Exception for C#

I'm using JSON.net in C# for an Excel VSTO Add in and pulling in JSON via web service. 我在C#中使用JSON.net进行Excel VSTO Add并通过Web服务引入JSON。

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. 我已验证我提取的JSON有效(在线JSON验证程序),但无法将JSON转换为C#中的对象以使用。

When I run the code below I get the exception below. 当我运行下面的代码时,我得到下面的异常。

Any ideas on who I can covert the JSON correctly? 关于谁可以正确隐瞒JSON的任何想法?

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: 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"]
}

The JSON represents a single instance of the object, not an array. JSON表示对象的单个实例 ,而不是数组。 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: 相反,如果它应该是一个数组,则用括号将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"]
}]

Edit: As others have also been pointing out, the properties on the JSON object don't actually match that class definition. 编辑:正如其他人也指出的那样,JSON对象上的属性实际上与该类定义不匹配。 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. 因此,尽管它可以“成功”进行反序列化,但这样做会忽略它不关心的JSON属性,并将类属性初始化为默认值。

Perhaps you meant to use a different class? 也许您打算使用其他类? Or different JSON? 还是其他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. 但是在纠正该问题时,您将继续进行下一个问题。

The RootObject and the json are not compatible. RootObject和json不兼容。 You could deserialize using a dictionary. 您可以使用字典进行反序列化。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM