Newtonsoft JSON deserialiazing为null

时间:2014-02-04 15:13:13

标签: c# json.net

我创建了一个Log类,模仿了W3C所描述的HAR文件格式的所有属性。这是一个JSON格式的文件。我有一个由Chrome生成的有效HAR文件,我正在尝试使用Newtonsoft Json反序列化到该Log对象中。

我遇到的问题是我的对象始终为null,并且不会抛出任何错误。我的班级看起来像这样:

public class Log
{
    public int Version { get; set; }
    public Creator Creator { get; set; }
    public IList<Page> Pages { get; set; }
    public IList<Entry> Entries { get; set; }
    public Browser Browser { get; set; }       
    public string Comment { get; set; }

    public Log(){}
}

我的代码现在很简单,看起来像这样:

static void Main(string[] args)
        {
            try
            {
                Log myLog = JsonConvert.DeserializeObject<Log>(File.ReadAllText(@"C:\Users\Me\some.har"));
                Console.WriteLine("Parsed.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while parsing: " + ex.InnerException);
            }
        }

没有异常被捕获,我的myLog始终为空。我按照official documentation中的示例进行了操作。

我想知道的是:

  1. 我班级中的属性顺序必须与JSON / HAR文件匹配吗?
  2. 由于我创建了自己的子类(Creator,Page,Entry,Browser),Deserializer会理解格式吗?
  3. 编辑:样本HAR

    {
      "log": {
        "version": "1.2",
        "creator": {
          "name": "WebInspector",
          "version": "537.36"
        },
        "pages": [
          {
            "startedDateTime": "2014-01-21T08:24:57.942Z",
            "id": "page_1",
            "title": "http://somewebsite.net/",
            "pageTimings": {
              "onContentLoad": 1903.4998416900635,
              "onLoad": 3104.4998168945312
            }
          }
        ],
        "entries": [
          {
            "startedDateTime": "2014-01-21T08:24:57.942Z",
            "time": 503.49998474121094,
            "request": {
              "method": "GET",
              "url": "http://somewebsite.net/",
              "httpVersion": "HTTP/1.1",
              "headers": [
                {
                  "name": "DNT",
                  "value": "1"
                },
                {
                  "name": "Accept-Encoding",
                  "value": "gzip,deflate,sdch"
                },
                {
                  "name": "Host",
                  "value": "somewebsite.net"
                },
                {
                  "name": "Accept-Language",
                  "value": "en-US,en;q=0.8,fr;q=0.6"
                },
                {
                  "name": "User-Agent",
                  "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
                },
                {
                  "name": "Accept",
                  "value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
                },
                {
                  "name": "Cookie",
                  "value": "some cookie values"
                },
                {
                  "name": "Connection",
                  "value": "keep-alive"
                }
              ],
              "queryString": [],
              "cookies": [
                {
                  "name": "__utma",
                  "value": "102234624.306294763.1379404578.1384330947.1384425583.24",
                  "expires": null,
                  "httpOnly": false,
                  "secure": false
                },
                {
                  "name": "__utmz",
                  "value": "102234624.1381933765.18.3.utmcsr=cloud.feedly.com|utmccn=(referral)|utmcmd=referral|utmcct=/",
                  "expires": null,
                  "httpOnly": false,
                  "secure": false
                }
              ],
              "headersSize": 545,
              "bodySize": 0
            },
            "response": {
              "status": 200,
              "statusText": "OK",
              "httpVersion": "HTTP/1.1",
              "headers": [
                {
                  "name": "Date",
                  "value": "Tue, 21 Jan 2014 08:24:55 GMT"
                },
                {
                  "name": "Content-Encoding",
                  "value": "gzip"
                },
                {
                  "name": "X-AspNetMvc-Version",
                  "value": "4.0"
                },
                {
                  "name": "Server",
                  "value": "Microsoft-IIS/8.0"
                },
                {
                  "name": "X-AspNet-Version",
                  "value": "4.0.30319"
                },
                {
                  "name": "X-Powered-By",
                  "value": "ASP.NET"
                },
                {
                  "name": "Vary",
                  "value": "Accept-Encoding"
                },
                {
                  "name": "Content-Type",
                  "value": "text/html; charset=utf-8"
                },
                {
                  "name": "Cache-Control",
                  "value": "private"
                },
                {
                  "name": "Set-Cookie",
                  "value": "ARRAffinity=626d4c1ec24d15067ac8f55d59e3408e76534534521b3fe88ca8293a385ba23f;Path=/;Domain=somewebsite.net"
                },
                {
                  "name": "Set-Cookie",
                  "value": "WAWebSiteSID=bf378e651bd145f097244bb697d74996; Path=/; HttpOnly"
                },
                {
                  "name": "Content-Length",
                  "value": "6205"
                }
              ],
              "cookies": [
                {
                  "name": "ARRAffinity",
                  "value": "626d4c1ec24d15067ac8f55d59e3408e76534534521b3fe88ca8293a385ba23f",
                  "path": "/",
                  "domain": "somewebsite.net",
                  "expires": null,
                  "httpOnly": false,
                  "secure": false
                },
                {
                  "name": "WAWebSiteSID",
                  "value": "bf378e651bd145f097244bb697d74996",
                  "path": "/",
                  "expires": null,
                  "httpOnly": true,
                  "secure": false
                }
              ],
              "content": {
                "size": 18380,
                "mimeType": "text/html",
                "compression": 12175,
                "text": "htmlcontent"
              },
              "redirectURL": "",
              "headersSize": 506,
              "bodySize": 6205
            },
            "cache": {},
            "timings": {
              "blocked": 16.999999999995907,
              "dns": 50.99999999998772,
              "connect": 108.00000000000409,
              "send": 0,
              "wait": 217.00000000004138,
              "receive": 110.49998474118183,
              "ssl": -1
            },
            "connection": "3557",
            "pageref": "page_1"
          },
          {
            "startedDateTime": "2014-01-21T08:24:58.593Z",
            "time": 846.4999198913574,
            "request": {
              "method": "GET",
              "url": "http://somewebsite.net/Content/bootstrap/bootstrap.css",
              "httpVersion": "HTTP/1.1",
              "headers": [
                {
                  "name": "DNT",
                  "value": "1"
                },
                {
                  "name": "Accept-Encoding",
                  "value": "gzip,deflate,sdch"
                },
                {
                  "name": "Host",
                  "value": "somewebsite.net"
                },
                {
                  "name": "Accept-Language",
                  "value": "en-US,en;q=0.8,fr;q=0.6"
                },
                {
                  "name": "User-Agent",
                  "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
                },
                {
                  "name": "Accept",
                  "value": "text/css,*/*;q=0.1"
                },
                {
                  "name": "Referer",
                  "value": "http://somewebsite.net/"
                },
                {
                  "name": "Cookie",
                  "value": "__utma=102234624.306294763.1379404578.1384330947.1384425583.24; __utmz=102234624.1381933765.18.3.utmcsr=cloud.feedly.com|utmccn=(referral)|utmcmd=referral|utmcct=/; ARRAffinity=626d4c1ec24d15067ac8f55d59e3408e76534534521b3fe88ca8293a385ba23f; WAWebSiteSID=bf378e651bd145f097244bb697d74996"
                },
                {
                  "name": "Connection",
                  "value": "keep-alive"
                }
              ],
              "queryString": [],
              "cookies": [
                {
                  "name": "__utma",
                  "value": "102234624.306294763.1379404578.1384330947.1384425583.24",
                  "expires": null,
                  "httpOnly": false,
                  "secure": false
                },
                {
                  "name": "__utmz",
                  "value": "102234624.1381933765.18.3.utmcsr=cloud.feedly.com|utmccn=(referral)|utmcmd=referral|utmcct=/",
                  "expires": null,
                  "httpOnly": false,
                  "secure": false
                },
                {
                  "name": "ARRAffinity",
                  "value": "626d4c1ec24d15067ac8f55d59e3408e76534534521b3fe88ca8293a385ba23f",
                  "expires": null,
                  "httpOnly": false,
                  "secure": false
                },
                {
                  "name": "WAWebSiteSID",
                  "value": "bf378e651bd145f097244bb697d74996",
                  "expires": null,
                  "httpOnly": false,
                  "secure": false
                }
              ],
              "headersSize": 694,
              "bodySize": 0
            },
            "response": {
              "status": 200,
              "statusText": "OK",
              "httpVersion": "HTTP/1.1",
              "headers": [
                {
                  "name": "Date",
                  "value": "Tue, 21 Jan 2014 08:24:56 GMT"
                },
                {
                  "name": "Content-Encoding",
                  "value": "gzip"
                },
                {
                  "name": "Last-Modified",
                  "value": "Sat, 14 Dec 2013 11:24:09 GMT"
                },
                {
                  "name": "Server",
                  "value": "Microsoft-IIS/8.0"
                },
                {
                  "name": "X-Powered-By",
                  "value": "ASP.NET"
                },
                {
                  "name": "ETag",
                  "value": "\"67d4af1bff8ce1:0\""
                },
                {
                  "name": "Vary",
                  "value": "Accept-Encoding"
                },
                {
                  "name": "Content-Type",
                  "value": "text/css"
                },
                {
                  "name": "Accept-Ranges",
                  "value": "bytes"
                },
                {
                  "name": "Content-Length",
                  "value": "26323"
                }
              ],
              "cookies": [],
              "content": {
                "size": 126344,
                "mimeType": "text/css",
                "compression": 100021,
                "text": "some content"
              },
              "redirectURL": "",
              "headersSize": 294,
              "bodySize": 26323
            },
            "cache": {},
            "timings": {
              "blocked": 276.00000000001046,
              "dns": -1,
              "connect": -1,
              "send": 0,
              "wait": 452.99999999997453,
              "receive": 117.49991989137243,
              "ssl": -1
            },
            "connection": "3557",
            "pageref": "page_1"
          },
        ]
      }
    }
    

1 个答案:

答案 0 :(得分:1)

你需要一个包装类:

class Wrapper
{
    public Log log { get; set; }
}

然后像这样反序列化:

string json = File.ReadAllText(@"C:\Users\Me\some.har");
Log myLog = JsonConvert.DeserializeObject<Wrapper>(json).Log;