JSON.NET到C#对象

前端之家收集整理的这篇文章主要介绍了JSON.NET到C#对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在 Windows窗体中使用 JSON.NET框架从 JSON字符串中读取一些信息.但我努力从“分类法 – >主题”数组和“集群”中获取字典
{
    "keywords": {
        "anyString": [

        ],"allString": {
            "a5349f533e3aa3ccbc27de2638da38d6": "olympics"
        },"exactString": [

        ],"notString": [

        ],"highlightString": [

        ]
    },"dates": {
        "startDate": "15-01-2008","endDate": "15-09-2009","useDates": true
    },"clusters": {
        "permission": {
            "1": "private\/n"
        }
    },"taxonomies": {
        "Topics": {
            "2488": "Olympics 2012 (not participation)","8876": "Olympics and culture"
        },"Keywords": {
            "8848": "Engineering in the Olympics"
        }
    },"sort": {
        "sortId": 1,"sortType": 2,"sort": "datetime","sortOrder": "descending"
    }
}

使用下面的代码,我已经能够阅读一些信息.

JObject searchCriteria = JObject.Parse(contentSearchCriteria);  
//search criteria   
IEnumerable<string> allString = searchCriteria["keywords"]["allString"].Children().Values<string>();
IEnumerable<string> anyString = searchCriteria["keywords"]["anyString"].Children().Values<string>();
IEnumerable<string> notString = searchCriteria["keywords"]["notString"].Children().Values<string>();
IEnumerable<string> exactString = searchCriteria["keywords"]["exactString"].Children().Values<string>();
IEnumerable<string> highlightString = searchCriteria["keywords"]["highlightString"].Children().Values<string>();
//dates
string startDate = (string)searchCriteria["dates"]["startDate"];
string endDate = (string)searchCriteria["dates"]["endDate"];
bool useDates = (bool)searchCriteria["dates"]["useDates"];

//sort
int sortId = (int)searchCriteria["sort"]["sortId"];
int sortType = (int)searchCriteria["sort"]["sortType"];
string sort = (string)searchCriteria["sort"]["sort"];
string sortOrder = (string)searchCriteria["sort"]["sortOrder"];

更新:

正如我所建议的那样

class SMSearchCriteria
    {
        public SMKeywords keywords { get; set; }
        public SMDates dates { get; set; }
        public SMClusters clusters { get; set; }
        public SMTaxonomies taxonomies { get; set; }
        public SMSort sort { get; set; }
    }

    class SMKeywords
    {
        public List<Dictionary<string,string>> AnyString {get; set;}
        public List<Dictionary<string,string>> AllString { get; set; }
        public List<Dictionary<string,string>> ExactString { get; set; }
        public List<Dictionary<string,string>> NotString { get; set; }
        public List<Dictionary<string,string>> HighlightString { get; set; } 
    }

    class SMDates
    {
        public string startDate { get; set; }
        public string endDate { get; set; }
        public bool useDates { get; set; }
    }

    class SMClusters
    {
        List<SMCluster> cluster;
    }

    class SMCluster
    {
        public Dictionary<string,string> cluster { get; set; }
    }

     class SMTaxonomies
    {
         public List<SMTaxonomy> taxonomies { get; set; }
    }

     class SMTaxonomy
     {
         public Dictionary<string,List<SMCategory>> taxonomy { get; set; }
     }

     class SMCategory
     {
         public Dictionary<int,string> category { get; set; }
     }

     class SMSort
    {
        public int sortId { get; set; }
        public int sortType { get; set; }
        public string sort { get; set; }
        public string sortOrder { get; set; }
    }

但是当我执行时:

var mydata = JsonConvert.DeserializeObject<SMSearchCriteria>(contentSearchCriteria);

我得到了例外:

[Newtonsoft.Json.JsonSerializationException] = {"Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.String,System.String]]'."}

更新2:

正如所建议的那样,我删除了所有额外的列表并将类简化为:

class SearchMasterSearchCriteria
    {
        public SMKeywords keywords { get; set; }
        public SMDates dates { get; set; }
        public Dictionary<string,Dictionary<int,string>> clusters { get; set; } 
        public Dictionary<string,string>> taxonomies { get; set; } 
        public SMSort sort { get; set; }
    }

    class SMKeywords
    {
        public Dictionary<string,string> anyString {get; set;}
        public Dictionary<string,string> allString { get; set; }
        public Dictionary<string,string> exactString { get; set; }
        public Dictionary<string,string> notString { get; set; }
        public Dictionary<string,string> highlightString { get; set; } 
    }

    class SMDates
    {
        public string startDate { get; set; }
        public string endDate { get; set; }
        public bool useDates { get; set; }
    }

     class SMSort
    {
        public int sortId { get; set; }
        public int sortType { get; set; }
        public string sort { get; set; }
        public string sortOrder { get; set; }
    }

我还添加了测试代码来序列化对象,如下所示:

//criteria
            SearchMasterSearchCriteria smCriteria = new SearchMasterSearchCriteria();

            //keywords
            SMKeywords smKeywords = new SMKeywords(); ;
            Dictionary<string,string> dict = new Dictionary<string,string>();
            dict.Add("a5349f533e3aa3ccbc27de2638da38d6","olympics");
            dict.Add("9cfa7aefcc61936b70aaec6729329eda","games");
            smKeywords.allString = dict;

            //category
            Dictionary<int,string> categorieDict = new Dictionary<int,string>();
            categorieDict.Add(2488,"Olympics 2012 (not participation)");
            categorieDict.Add(8876,"Olympics and culture");

            //taxonomies
            Dictionary<string,string>> taxonomiesDict = new Dictionary<string,string>>();
            taxonomiesDict.Add("Topics",categorieDict);

            //Metadata
            Dictionary<int,string> MetadataDict = new Dictionary<int,string>();
            MetadataDict.Add(1,@"private/n");

            //clusters
            Dictionary<string,string>> clustersDict = new Dictionary<string,string>>();
            clustersDict.Add("permission",MetadataDict);


            //dates
            SMDates smDates = new SMDates();
            smDates.startDate = "15-01-2008";
            smDates.endDate = "15-09-2009";
            smDates.useDates = true;

            //sort
            SMSort smSort = new SMSort();
            smSort.sortId = 1;
            smSort.sortType = 2;
            smSort.sort = "datetime";
            smSort.sortOrder = "descending";

           //add to criteria.
            smCriteria.keywords = smKeywords;
            smCriteria.clusters = clustersDict;
            smCriteria.taxonomies = taxonomiesDict;
            smCriteria.dates = smDates;
            smCriteria.sort = smSort;

            //serialize
            string json = JsonConvert.SerializeObject(smCriteria);
            var mydata1 = JsonConvert.DeserializeObject<SearchMasterSearchCriteria>(json);

到那时候2 json字符串之间的唯一区别在哪里. anyString,exactString等的[]和nulls所以我用空的方括号代替了卷曲的方括号,并且它没有错误地被去除了:)

contentSearchCriteria = contentSearchCriteria.Replace("[]","{}");
var mydata = JsonConvert.DeserializeObject<SearchMasterSearchCriteria>(contentSearchCriteria);
说实话,我不会这样做,你完全是这样做的.这是我检索数据的方式:
class Data {
     Dictionary<string,Dictionary<string,string>> keywords;
     DatesClass dates;
     .......

}

class DatesClass
{
    string startDate;
    string endDate;
    bool? useDates

}


var mydata = JsonConvert.DeserializeObject<Data>(jsonstring);

我没有填写整个数据类,但你明白了.我发现在输入数据的结构中创建一个对象然后使用DeserializeObject方法来填充数据要容易得多.这也使代码更清晰,并允许编译器检查拼写错误.

原文链接:https://www.f2er.com/json/288472.html

猜你在找的Json相关文章