2016-06-01 19 views
0

So verwende ich JSON.net für ein Forschungsprojekt. Meine Beispiel-JSON-Datei sieht so aus. Die Datei ist riesig, aber Geduld mit mir, ich brauche nur einen Teil davon zu verarbeiten:Grundlegende JSON Parsing-Abfrage

{ 
    "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." 
    } 
    ] 
} 

Also ich mag nur die name in das Ausgabefenster zu drucken, so habe ich die einfache CName Klasse. Hier ist mein Code:

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); 
} 

EDIT: Scheint mein Verständnis von JSON wirklich schlecht ist. Was ich versuche zu tun, ist dies: von "name" : "ECDHE_RSA_WITH_RC4_128_SHA", und alle ähnlichen Linien mit "name", ich brauche diese ECDHE_RSA_WITH_RC4_128_SHA Typ Werte, die ich nicht bekomme. Aber alles, was ich bekomme, ist eine leere Ausgabe. Vorschläge?

Danke.

+1

Es gibt keine 'name' -Eigenschaft im Stammverzeichnis Ihres JSON. –

+0

@PaulAbbott Ich bin neu, wie kann ich dann auf 'name' zugreifen? –

+0

was "name"? wie @PaulAbbott sagte, es gibt keine "Name" -Eigenschaft auf Ihrem JSON-Objekt – Gusman

Antwort

0

Wenn Sie nur einen kleinen Teil des Inhalts möchten und kein vollständiges Modell erstellen möchten, können Sie DeserializeObject() anstelle von DeserializeObject<T>() verwenden, wodurch ein JToken zurückgegeben wird, bei dem es sich um ein Objekt mit den erforderlichen Funktionen handelt , greifen Sie auf Mitglieder wie Arrays usw. zu.

Here ist ein Link zur Dokumentation von JToken.