我试图将一个存储在文件中的 json 数据保存到一个SearchResults
类下面的对象中:
JSON:
{
"Collapsing": {
"CollapsedDocuments": {}
},
"FacetDates": {},
"FacetFields":{
"TYPE": {
"DA": 1,
"FA": 2
}
},
"FacetQueries": {},
"Header": {
"params": {
"limit": -1,
"facet": true,
"sort": "",
"mincount": 1,
"start": 0,
"q": "",
"field": "",
"fq": "",
"rows": 1
},
"QTime": 3,
"Status": 0
},
"Highlights": null,
"IsReadOnly": false,
"MaxScore": null,
"NumFound": 1,
"SimilarResults": {},
"SpellChecking": {
"Collation": null,
"Count": 1,
"IsReadOnly": false
},
"Stats": {}
}
守则:
public class SearchResults
{
public IEnumerable<CustomResultData> ResultData { get; set; }
public IDictionary<string, ICollection<KeyValuePair<string, int>>> MyFields { get; set; }
public IDictionary<string, int> MyQueries { get; set; }
public int QueryTime { get; set; }
public int TotalResults { get; set; }
public IDictionary<string, IDictionary<string, ICollection<string>>> Highlights;
}
public class CustomResultData
{
[SolrUniqueKey("id")]
public string ID { get; set; }
[SolrUniqueKey("attr1")]
public ICollection<string> attr1 { get; set; }
[SolrField("attr2")]
public ICollection<string> attr2 { get; set; }
}
下面的代码是数据转换的代码:
string jsonResponseStr;
jsonResponseStr = File.ReadAllText(HttpContext.Current.Server.MapPath("~/my_json_data.json"));
JObject jObject = JsonConvert.DeserializeObject<JObject>(jsonResponseStr);
var ResultData = jObject;
var FacetFields = (IDictionary<string, ICollection<KeyValuePair<string, int>>>)jObject["FacetFields"];
var FacetQueries = (IDictionary<string, int>)jObject["FacetQueries"];
var QueryTime = (int)jObject["QueryTime"];
var TotalResults = (int)jObject["TotalResults"];
var Highlights = (IDictionary<string, IDictionary<string, ICollection<string>>>)jObject["Highlights"];
searchResults = new SearchResults
{
FacetFields = FacetFields, // <<---- Error
ResultData = ResultData, // <<---- Error
FacetQueries = FacetQueries, // <<---- Error
QueryTime = QueryTime,
TotalResults = TotalResults,
Highlights = Highlights // <<---- Error
};
我在所有初始化行(上面标记为**〈〈---- Error**)都得到运行时错误Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.IDictionary
。
谁能给予我代码中有什么问题吗?
1条答案
按热度按时间taor4pac1#
你为什么不这么做
阶级