代码之家  ›  专栏  ›  技术社区  ›  Bokambo

反序列化不带字段名的JSON

  •  0
  • Bokambo  · 技术社区  · 6 年前

    我有下面的JSON。当我反序列化它时, responseHeader response highlighting 没有。

    问题是突出显示如下所示的数据, _text_

    1. C:\\TestArea..........
    2. _text_
    
      "highlighting":{ 
            "C:\\TestArea\\Destination\\SUP000011\\ATM-1B4L2KQ0ZE0-0001\\SoS_Update_2018_06_04_pram.pptx":{
              "_text_":["\nSCRUM of SCRUMs S4-W1 \n AXP Internal \n 4-<em>Jun</em>-18 \n \n slide-master-content   \n \n  \n slide-notes   \n 4-<em>Jun</em>"]},
    

    JSON码:

    {
        "responseHeader": {
            "status": 0,
            "QTime": 115,
            "params": {
                "q": "\"John\"",
                "hl": "on",
                "hl.simple.post": "</em>",
                "start": "0",
                "rows": "10000",
                "hl.simple.pre": "<em>"
            }
        },
        "response": {
            "numFound": 10,
            "start": 0,
            "docs": [{
                "domain": ["FIU/FCRU"],
                "id": "C:\\TestArea\\Destination\\SUP000011\\ATM-1B4L2KQ0ZE0-0001\\SoS_Update_2018_06_04_pram.pptx"
    
            }]
        },
        "highlighting": {
            "C:\\TestArea\\Destination\\SUP000011\\ATM-1B4L2KQ0ZE0-0001\\SoS_Update_2018_06_04_pram.pptx": {
                "_text_": ["\nSCRUM of SCRUMs S4-W1 \n AXP Internal \n 4-<em>Jun</em>-18 \n \n slide-master-content   \n \n  \n slide-notes   \n 4-<em>Jun</em>"]
            },
            "C:\\TestArea\\Destination\\SUP000005\\F-3-20150505-0028\\tt.csv": {
                "_text_": [",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL\r\n930,<em>John</em> Ferguson,NULL,2015-06-30 15:27:04.677,test.test"]
    
            }
        }
    }
    

    public class RootObject
    {
        public ResponseHeader responseHeader { get; set; }
        public Response response { get; set; }
        public Highlighting highlighting { get; set; }
    } 
    public class Highlighting
    {   
        public List<string> _text_ { get; set; }
    }
    
    var outObject = JsonConvert.DeserializeObject<RootObject>(jsonString, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   dbc    6 年前

    对于 highlighting 属性使用 Dictionary<string, Highlighting> 像这样:

    public class RootObject
    {
        public ResponseHeader responseHeader { get; set; }
        public Response response { get; set; }
        public Dictionary<string, Highlighting> highlighting { get; set; }
    } 
    
    public class Highlighting
    {   
        public List<string> _text_ { get; set; }
    }
    

    "highlighting" 像这样:

    "highlighting": {
       "File Name 1":{ "_text_":[ "text 1" ] },
       "File name 2":{ "_text_":[ "text 2" ] }
    }
    

    我们可以看到,它是一个JSON对象,具有可变属性名称,其值具有固定的模式。Json.NET支持在字典中序列化;有关详细信息,请参阅 Deserialize a Dictionary .

    样品提琴 here