我在C#中使用JSON.net作爲Excel VSTO添加並通過Web服務拉入JSON。.NET的JSON序列化例外C#
我已驗證JSON我拉是有效的(在線JSON驗證器),但無法將JSON轉換爲C#中的對象使用。
當我運行下面的代碼時,我得到下面的異常。
關於誰可以正確隱藏JSON的任何想法?
例外:
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.
代碼:
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"]
}
您正試圖反序列化一個RootObjects數組,但它只是一個對象。將RootObject []更改爲RootObject – Nico
聽起來好像你在問什麼是「數組」這個詞。但錯誤消息*實際上告訴你它的含義*。 –
此外,您的對象定義與您的JSON不匹配 – Nico