jeet jeet - 6 months ago 13
JSON Question

Basic JSON Parsing Query

So I am using JSON.net for a research based project. My sample JSON file looks like this. The file is huge but bear with me, I just need to process one part of it:

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


So I just want to print the
name
to the output window so I created the simple
CName
class. Here is my 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: Seems my understanding of JSON is really poor. What I am trying to do is this: from
"name" : "ECDHE_RSA_WITH_RC4_128_SHA",
and all similar lines with
"name"
, I need those
ECDHE_RSA_WITH_RC4_128_SHA
type values that I fail to get.
However all I get is blank output.
Suggestions?

Thanks.

Answer

If you just want an small part of the content and don't want to create a full model, you can use DeserializeObject() instead of DeserializeObject<T>(), that will return a JToken which is an object with the needed functions to iterate over it, access it's members like arrays, etc.

Here is a link to the documentation of JToken.