基本JSON解析查询

时间:2016-06-01 22:08:42

标签: c# json

所以我使用JSON.net进行基于研究的项目。我的示例JSON文件看起来像这样。文件很大但是请耐心等待,我只需要处理它的一部分:

{
  "connectionName" : "example.com",
  "connectionPort" : 443,
  "SNI" : "example.com",
  "SSLv3" : {
    "suiteSelection" : "server",
    "suites" : [
      {
        "id" : 49169,
        "name" : "ECDHE_RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49171,
        "name" : "ECDHE_RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49172,
        "name" : "ECDHE_RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 47,
        "name" : "RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 53,
        "name" : "RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 5,
        "name" : "RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      }
    ]
  },
  "TLSv1.0" : {
    "suiteSelection" : "server",
    "suites" : [
      {
        "id" : 49169,
        "name" : "ECDHE_RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49171,
        "name" : "ECDHE_RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49172,
        "name" : "ECDHE_RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 47,
        "name" : "RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 53,
        "name" : "RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 5,
        "name" : "RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      }
    ]
  },
  "TLSv1.1" : {
    "suiteSelection" : "server",
    "suites" : [
      {
        "id" : 49169,
        "name" : "ECDHE_RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49171,
        "name" : "ECDHE_RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49172,
        "name" : "ECDHE_RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 47,
        "name" : "RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 53,
        "name" : "RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 5,
        "name" : "RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      }
    ]
  },
  "TLSv1.2" : {
    "suiteSelection" : "server",
    "suites" : [
      {
        "id" : 49199,
        "name" : "ECDHE_RSA_WITH_AES_128_GCM_SHA256",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49191,
        "name" : "ECDHE_RSA_WITH_AES_128_CBC_SHA256",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49169,
        "name" : "ECDHE_RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49171,
        "name" : "ECDHE_RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49172,
        "name" : "ECDHE_RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49200,
        "name" : "ECDHE_RSA_WITH_AES_256_GCM_SHA384",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 49192,
        "name" : "ECDHE_RSA_WITH_AES_256_CBC_SHA384",
        "strength" : 3,
        "forwardSecrecy" : true,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 156,
        "name" : "RSA_WITH_AES_128_GCM_SHA256",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 157,
        "name" : "RSA_WITH_AES_256_GCM_SHA384",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 60,
        "name" : "RSA_WITH_AES_128_CBC_SHA256",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 47,
        "name" : "RSA_WITH_AES_128_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 61,
        "name" : "RSA_WITH_AES_256_CBC_SHA256",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 53,
        "name" : "RSA_WITH_AES_256_CBC_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      },
      {
        "id" : 5,
        "name" : "RSA_WITH_RC4_128_SHA",
        "strength" : 3,
        "forwardSecrecy" : false,
        "anonymous" : false,
        "serverKeyType" : "RSA"
      }
    ]
  },
  "ssl3Chains" : [
    {
      "length" : 3,
      "decoded" : true,
      "namesMatch" : true,
      "includesRoot" : false,
      "signHashes" : [
        "SHA-1",
        "SHA-256"
      ],
      "certificates" : [
        {
          "thumbprint" : "4F851C51052200FE11C8BBFA1ED4BD29CFC7CB16",
          "decodable" : true,
          "serialHex" : "773A528C676E2348E3E6ED494D6A5EC9",
          "subject" : "CN=www.example.cn,OU=service operation department,O=BeiJing example Netcom Science Technology Co.\\, Ltd,L=beijing,ST=beijing,C=CN",
          "issuer" : "CN=Symantec Class 3 Secure Server CA - G4,OU=Symantec Trust Network,O=Symantec Corporation,C=US",
          "validFrom" : "2016-04-10 00:00:00 UTC",
          "validTo" : "2017-04-11 23:59:59 UTC",
          "keyType" : "RSA",
          "keySize" : 2048,
          "signHash" : "SHA-256",
          "selfIssued" : false,
          "serverNames" : [
            "example.cn",
            "example.com",
            "example.com.cn",
            "w.example.com",
            "ww.example.com",
            "www.example.cn",
            "www.example.com.cn",
            "www.example.com.hk",
            "www.example.hk",
            "www.example.net.au",
            "www.example.net.my",
            "www.example.net.ph",
            "www.example.net.pk",
            "www.example.net.tw",
            "www.example.net.vn",
            "wwww.example.com",
            "wwww.example.com.cn"
          ]
        },
        {
          "thumbprint" : "FF67367C5CD4DE4AE18BCCE1D70FDABD7C866135",
          "decodable" : true,
          "serialHex" : "513FB9743870B73440418D30930699FF",
          "subject" : "CN=Symantec Class 3 Secure Server CA - G4,OU=Symantec Trust Network,O=Symantec Corporation,C=US",
          "issuer" : "CN=VeriSign Class 3 Public Primary Certification Authority - G5,OU=(c) 2006 VeriSign\\, Inc. - For authorized use only,OU=VeriSign Trust Network,O=VeriSign\\, Inc.,C=US",
          "validFrom" : "2013-10-31 00:00:00 UTC",
          "validTo" : "2023-10-30 23:59:59 UTC",
          "keyType" : "RSA",
          "keySize" : 2048,
          "signHash" : "SHA-256",
          "selfIssued" : false
        },
        {
          "thumbprint" : "32F30882622B87CF8856C63DB873DF0853B4DD27",
          "decodable" : true,
          "serialHex" : "250CE8E030612E9F2B89F7054D7CF8FD",
          "subject" : "CN=VeriSign Class 3 Public Primary Certification Authority - G5,OU=(c) 2006 VeriSign\\, Inc. - For authorized use only,OU=VeriSign Trust Network,O=VeriSign\\, Inc.,C=US",
          "issuer" : "OU=Class 3 Public Primary Certification Authority,O=VeriSign\\, Inc.,C=US",
          "validFrom" : "2006-11-08 00:00:00 UTC",
          "validTo" : "2021-11-07 23:59:59 UTC",
          "keyType" : "RSA",
          "keySize" : 2048,
          "signHash" : "SHA-1",
          "selfIssued" : false
        }
      ]
    }
  ],
  "deflateCompress" : false,
  "serverTime" : "random",
  "secureRenegotiation" : true,
  "minECSize" : 256,
  "minECSizeExt" : 256,
  "namedCurves" : [
    {
      "name" : "secp256r1 (P-256)",
      "size" : 256,
      "spontaneous" : true
    }
  ],
  "warnings" : [
    {
      "id" : "CS005",
      "text" : "Server supports RC4."
    },
    {
      "id" : "CS006",
      "text" : "Server supports cipher suites with no forward secrecy."
    },
    {
      "id" : "PV003",
      "text" : "Server supports SSL 3.0."
    },
    {
      "id" : "XC001",
      "text" : "Server certificate was signed with a weak/deprecated/unknown hash function."
    }
  ]
}

所以我只想将name打印到输出窗口,这样我就创建了简单的CName类。这是我的代码:

public class CName
{
     public string name { get; set; }
}

DirectoryInfo dinfo = new DirectoryInfo(@"C:\output_v_2\");
FileInfo[] Files = dinfo.GetFiles("*.json");

foreach (FileInfo file in Files)
{
    CName read_data = JsonConvert.DeserializeObject<CName>(File.ReadAllText(file.Name));
    Debug.WriteLine(read_data.name);
}

编辑:似乎我对JSON的理解真的很差。我想要做的是:从"name" : "ECDHE_RSA_WITH_RC4_128_SHA",和所有与"name"相似的行,我需要那些我无法获得的ECDHE_RSA_WITH_RC4_128_SHA类型值。 但是我得到的只是空白输出。 建议?

感谢。

1 个答案:

答案 0 :(得分:0)

如果您只想要一小部分内容并且不想创建完整模型,则可以使用DeserializeObject()代替DeserializeObject<T>(),这将返回一个JToken,这是一个具有所需函数的对象,以迭代它,访问它的成员,如数组等。

Here是JToken文档的链接。