asp.net 无法将"Newtonsoft.Json.Linq.JObject“类型的对象强制转换为”System.Collections.Generic.IDictionary“类型

0qx6xfy6  于 2022-12-15  发布在  .NET
关注(0)|答案(1)|浏览(329)

我试图将一个存储在文件中的 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
谁能给予我代码中有什么问题吗?

taor4pac

taor4pac1#

你为什么不这么做

SearchResults searchResults= JsonConvert.DeserializeObject<SearchResults>(jsonResponseStr);

阶级

public class SearchResults
{
    public Collapsing Collapsing { get; set; }
    public FacetDates FacetDates { get; set; }
    public Dictionary<string, Dictionary<string, int>> FacetFields { get; set; }
    public Dictionary<string, int> FacetQueries { get; set; }
    public Header Header { get; set; }
    public object Highlights { get; set; }
    public bool IsReadOnly { get; set; }
    public object MaxScore { get; set; }
    public int NumFound { get; set; }
    public SimilarResults SimilarResults { get; set; }
    public Dictionary<string, object> SpellChecking { get; set; }
    public Stats Stats { get; set; }
}
public class Header
{
    [JsonProperty("params")]
    public Dictionary<string, object> Parameters { get; set; }
    public int QTime { get; set; }
    public int Status { get; set; }
}

public class SpellChecking
{
    public object Collation { get; set; }
    public int Count { get; set; }
    public bool IsReadOnly { get; set; }
}

public class SimilarResults
{
}
public class Collapsing
{
    public CollapsedDocuments CollapsedDocuments { get; set; }
}
public class CollapsedDocuments
{
}
public class FacetDates
{
}
public class Stats
{
}

相关问题