0
我被困在一個我相信應該工作的步驟中。我有一個方法(在一個單獨的類中),它應該在處理JSON之後返回一個List作爲它的值。我要粘貼代碼跳過JSON配置的東西:將JSON轉換爲列表
public static dynamic CustInformation(string Identifier)
{
//SKIPPED JSON CONFIG STUFF (IT'S WORKING CORRECTLY)
var result = "";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
dynamic d;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return JsonConvert.DeserializeObject<List<Models.RootObject>>(result);
}
是用C#爲JSON轉換器產生的模型:
public class Record
{
public string idIdentifier { get; set; }
public string KnowName1 { get; set; }
public string KnowAddress1 { get; set; }
public string KnowRelation1 { get; set; }
public string KnowPhone1 { get; set; }
public string KnowName2 { get; set; }
public string KnowAddress2 { get; set; }
//.....skipped other variables
}
public class RootObject
{
public List<Record> record { get; set; }
}
我打電話來是這樣的方法:
var model = Classes.EndPoint.CustInformation(identifier);
但我得到這個錯誤每次:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Models.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.
Path 'record', line 1, position 10.
編輯:JSON
{
"record": [
{
Identifier": "DQRJO1Q0IQRS",
"KnowName1": "",
"KnowAddress1": "",
"KnowRelation1": "",
"KnowPhone1": "",
"KnowName2": "",
"KnowAddress2": "",
//.....MORE STYFF
}
]
}
錯誤很明顯:它期待一個json數組,並且你沒有提供這個。你打給電話的json是什麼? –
您正在序列化一個包含Record對象列表的根對象。但是,您正在反序列化包含記錄對象列表的根對象列表。你需要確保你序列化和反序列化到相同的類型。 – mason
發表了JSON Marc,它應該「排隊」 –