JSON:
"divisions": {
"ocd-division/country:us": {
"name": "United States",
"officeIndices": [
0,
1
]
},
"ocd-division/country:us/state:mo": {
"name": "Missouri",
"officeIndices": [
2,
11,
12
]
},
"ocd-division/country:us/state:mo/cd:6": {
"name": "Missouri's 6th congressional district",
"officeIndices": [
3
]
},
我正在尝试从上述JSON中提取Missouri's 6th congressional district
。我可以使用以下代码正确提取它:
Console.WriteLine(json.SelectToken("divisions").
SelectToken("ocd-division/country:us/state:mo/cd:6").SelectToken("name").ToString)
这很好用,但是您会看到ocd-division/country:us/state:mo/cd:6
对于其他州甚至州的其他部分将是不同的。我想知道为什么这不能解决问题...
Console.WriteLine(json.SelectToken("divisions").
SelectToken("ocd-division/country:us").Next.Next.SelectToken("name").ToString)
我认为,因为所有状态都将在us
中,所以我可以从那里开始,然后向前走两个兄弟姐妹到达Missouri's 6th congressional district
。
完整JSON:
{
"kind": "civicinfo#representativeInfoResponse",
"normalizedInput": {
"line1": "****** *** ***",
"city": "******",
"state": "**",
"zip": "*****"
},
"divisions": {
"ocd-division/country:us": {
"name": "United States",
"officeIndices": [
0,
1
]
},
"ocd-division/country:us/state:mo": {
"name": "Missouri",
"officeIndices": [
2,
4,
5,
8,
9,
10,
11,
12
]
},
"ocd-division/country:us/state:mo/cd:6": {
"name": "Missouri's 6th congressional district",
"officeIndices": [
3
]
},
"ocd-division/country:us/state:mo/county:clay": {
"name": "Clay County",
"officeIndices": [
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23
]
},
"ocd-division/country:us/state:mo/place:kearney": {
"name": "Kearney city"
},
"ocd-division/country:us/state:mo/sldl:38": {
"name": "Missouri State House district 38",
"officeIndices": [
7
]
},
"ocd-division/country:us/state:mo/sldu:12": {
"name": "Missouri State Senate district 12",
"officeIndices": [
6
]
}
},
"offices": [
{
"name": "President of the United States",
"divisionId": "ocd-division/country:us",
"levels": [
"country"
],
"roles": [
"headOfState",
"headOfGovernment"
],
"officialIndices": [
0
]
},
{
"name": "Vice-President of the United States",
"divisionId": "ocd-division/country:us",
"levels": [
"country"
],
"roles": [
"deputyHeadOfGovernment"
],
"officialIndices": [
1
]
},
{
"name": "United States Senate",
"divisionId": "ocd-division/country:us/state:mo",
"levels": [
"country"
],
"roles": [
"legislatorUpperBody"
],
"officialIndices": [
2,
3
]
},
{
"name": "United States House of Representatives MO-06",
"divisionId": "ocd-division/country:us/state:mo/cd:6",
"levels": [
"country"
],
"roles": [
"legislatorLowerBody"
],
"officialIndices": [
4
]
},
{
"name": "Governor",
"divisionId": "ocd-division/country:us/state:mo",
"levels": [
"administrativeArea1"
],
"roles": [
"headOfGovernment"
],
"officialIndices": [
5
]
},
{
"name": "Lieutenant Governor",
"divisionId": "ocd-division/country:us/state:mo",
"levels": [
"administrativeArea1"
],
"roles": [
"deputyHeadOfGovernment"
],
"officialIndices": [
6
]
},
{
"name": "MO State Senate District 12",
"divisionId": "ocd-division/country:us/state:mo/sldu:12",
"levels": [
"administrativeArea1"
],
"roles": [
"legislatorUpperBody"
],
"officialIndices": [
7
]
},
{
"name": "MO State House District 38",
"divisionId": "ocd-division/country:us/state:mo/sldl:38",
"levels": [
"administrativeArea1"
],
"roles": [
"legislatorLowerBody"
],
"officialIndices": [
8
]
},
{
"name": "State Supreme Court Judge",
"divisionId": "ocd-division/country:us/state:mo",
"officialIndices": [
9,
10,
11,
12,
13,
14,
15
]
},
{
"name": "State Treasurer",
"divisionId": "ocd-division/country:us/state:mo",
"officialIndices": [
16
]
},
{
"name": "Attorney General",
"divisionId": "ocd-division/country:us/state:mo",
"officialIndices": [
17
]
},
{
"name": "State Auditor",
"divisionId": "ocd-division/country:us/state:mo",
"officialIndices": [
18
]
},
{
"name": "Secretary of State",
"divisionId": "ocd-division/country:us/state:mo",
"officialIndices": [
19
]
},
{
"name": "Assessor",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
20
]
},
{
"name": "Treasurer",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
21
]
},
{
"name": "Circuit Clerk",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
22
]
},
{
"name": "Auditor",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
23
]
},
{
"name": "County Commissioner Chairman",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
24
]
},
{
"name": "County Clerk",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
25
]
},
{
"name": "Collector",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
26
]
},
{
"name": "Prosecuting Attorney",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
27
]
},
{
"name": "Sheriff",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
28
]
},
{
"name": "Recorder",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
29
]
},
{
"name": "Public Administrator",
"divisionId": "ocd-division/country:us/state:mo/county:clay",
"officialIndices": [
30
]
}
],
"officials": [
{
"name": "Donald J. Trump",
"address": [
{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}
],
"party": "Republican Party",
"phones": [
"(202) 456-1111"
],
"urls": [
"http://www.whitehouse.gov/"
],
"photoUrl": "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/PE%20Color.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "potus"
},
{
"type": "YouTube",
"id": "whitehouse"
}
]
},
{
"name": "Mike Pence",
"address": [
{
"line1": "The White House",
"line2": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"zip": "20500"
}
],
"party": "Republican Party",
"phones": [
"(202) 456-1111"
],
"urls": [
"http://www.whitehouse.gov/"
],
"photoUrl": "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/VPE%20Color.jpg",
"channels": [
{
"type": "GooglePlus",
"id": "+whitehouse"
},
{
"type": "Facebook",
"id": "whitehouse"
},
{
"type": "Twitter",
"id": "VP"
}
]
},
{
"name": "Roy Blunt",
"address": [
{
"line1": "260 Russell Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Republican Party",
"phones": [
"(202) 224-5721"
],
"urls": [
"http://www.blunt.senate.gov/"
],
"photoUrl": "http://bioguide.congress.gov/bioguide/photo/B/B000575.jpg",
"channels": [
{
"type": "Facebook",
"id": "SenatorBlunt"
},
{
"type": "Twitter",
"id": "RoyBlunt"
},
{
"type": "YouTube",
"id": "SenatorBlunt"
},
{
"type": "YouTube",
"id": "BluntforSenate2010"
}
]
},
{
"name": "Josh Hawley",
"address": [
{
"line1": "B40A Dirksen Senate Office Building",
"city": "Washington",
"state": "DC",
"zip": "20510"
}
],
"party": "Republican Party",
"phones": [
"(202) 224-6154"
],
"emails": [
"senator@hawley.senate.gov"
],
"channels": [
{
"type": "Twitter",
"id": "SenHawleyPress"
}
]
},
{
"name": "Sam Graves",
"address": [
{
"line1": "1135 Longworth House Office Building",
"city": "Washington",
"state": "DC",
"zip": "20515"
}
],
"party": "Republican Party",
"phones": [
"(202) 225-7041"
],
"urls": [
"http://graves.house.gov/"
],
"photoUrl": "https://graves.house.gov/sites/graves.house.gov/files/documents/Rep_Graves_Photo.JPG",
"channels": [
{
"type": "Facebook",
"id": "118514606128"
},
{
"type": "Twitter",
"id": "repsamgraves"
}
]
},
{
"name": "Mike Parson",
"address": [
{
"line1": "P.O. Box 720",
"city": "Jefferson City",
"state": "MO",
"zip": "65102"
}
],
"party": "Republican Party",
"phones": [
"(573) 751-3222"
],
"urls": [
"https://governor.mo.gov/"
],
"photoUrl": "https://ltgov.mo.gov/wp-content/uploads/2017/01/Parson-headshot.jpg",
"emails": [
"ltgovinfo@ltgov.mo.gov"
],
"channels": [
{
"type": "Twitter",
"id": "GovParsonMO"
}
]
},
{
"name": "Mike Kehoe",
"address": [
{
"line1": "201 W. Capital Avenue",
"line2": "Room 224",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Republican Party",
"phones": [
"(573) 751-4727"
],
"urls": [
"https://ltgov.mo.gov/"
],
"channels": [
{
"type": "Twitter",
"id": "LtGovMikeKehoe"
}
]
},
{
"name": "Dan Hegeman",
"address": [
{
"line1": "201 W CAPITOL AVE RM 332",
"line2": "JEFFERSON CTY",
"city": "",
"state": "MO",
"zip": "65101"
}
],
"party": "Republican Party",
"phones": [
"(573) 751-1415"
],
"urls": [
"http://www.senate.mo.gov/mem12"
],
"photoUrl": "http://www.senate.mo.gov/17web/wp-content/uploads/2015/11/Hegeman-200x300.jpg",
"emails": [
"Dan.Hegeman@senate.mo.gov"
]
},
{
"name": "Doug Richey",
"address": [
{
"line1": "201 W CAPITOL AVE 116 A-1",
"line2": "JEFFERSON CTY",
"city": "",
"state": "MO",
"zip": "65101"
}
],
"party": "Republican Party",
"phones": [
"(573) 751-2238"
],
"urls": [
"http://www.house.mo.gov/MemberDetails.aspx?district=38"
],
"emails": [
"Doug.Richey@house.mo.gov"
]
},
{
"name": "Patricia Breckenridge",
"address": [
{
"line1": "Missouri Supreme Court",
"line2": "207 West High Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Nonpartisan",
"phones": [
"(573) 751-4144"
],
"urls": [
"https://www.courts.mo.gov/page.jsp?id=499"
]
},
{
"name": "W. Brent Powell",
"address": [
{
"line1": "Missouri Supreme Court",
"line2": "207 West High Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Nonpartisan",
"phones": [
"(573) 751-4144"
],
"urls": [
"https://www.courts.mo.gov/page.jsp?id=113629"
]
},
{
"name": "Mary R. Russell",
"address": [
{
"line1": "Missouri Supreme Court",
"line2": "207 West High Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Nonpartisan",
"phones": [
"(573) 751-4144"
],
"urls": [
"https://www.courts.mo.gov/page.jsp?id=203"
]
},
{
"name": "George W. Draper, III",
"address": [
{
"line1": "Missouri Supreme Court",
"line2": "207 West High Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Nonpartisan",
"phones": [
"(573) 751-4144"
],
"urls": [
"https://www.courts.mo.gov/page.jsp?id=50536"
]
},
{
"name": "Paul C. Wilson",
"address": [
{
"line1": "Missouri Supreme Court",
"line2": "207 West High Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Nonpartisan",
"phones": [
"(573) 751-4144"
],
"urls": [
"https://www.courts.mo.gov/page.jsp?id=59001"
]
},
{
"name": "Zel M. Fischer",
"address": [
{
"line1": "Missouri Supreme Court",
"line2": "207 West High Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Nonpartisan",
"phones": [
"(573) 751-4144"
],
"urls": [
"https://www.courts.mo.gov/page.jsp?id=27084"
]
},
{
"name": "Laura Denvir Stith",
"address": [
{
"line1": "Missouri Supreme Court",
"line2": "207 West High Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Nonpartisan",
"phones": [
"(573) 751-4144"
],
"urls": [
"https://www.courts.mo.gov/page.jsp?id=183"
]
},
{
"name": "Scott Fitzpatrick",
"address": [
{
"line1": "P.O. Box 210",
"city": "Jefferson City",
"state": "MO",
"zip": "65102"
}
],
"party": "Republican Party",
"phones": [
"(573) 751-8533"
],
"urls": [
"https://www.treasurer.mo.gov/"
],
"emails": [
"info@treasurer.mo.gov"
],
"channels": [
{
"type": "Twitter",
"id": "MOTreasurer"
}
]
},
{
"name": "Eric Schmitt",
"address": [
{
"line1": "207 W. High Street",
"line2": "P.O. Box 899",
"city": "Jefferson City",
"state": "MO",
"zip": "65102"
}
],
"party": "Republican Party",
"phones": [
"(573) 751-3321"
],
"urls": [
"https://www.ago.mo.gov/"
],
"channels": [
{
"type": "Twitter",
"id": "AGEricSchmitt"
}
]
},
{
"name": "Nicole Galloway",
"address": [
{
"line1": "P.O. Box 869",
"city": "Jefferson City",
"state": "MO",
"zip": "65102"
}
],
"party": "Democratic Party",
"phones": [
"(800) 347-8597"
],
"urls": [
"https://auditor.mo.gov/"
],
"emails": [
"moaudit@auditor.mo.gov"
],
"channels": [
{
"type": "Twitter",
"id": "AuditorGalloway"
}
]
},
{
"name": "John (Jay) Ashcroft",
"address": [
{
"line1": "600 West Main Street,",
"city": "Jefferson City",
"state": "MO",
"zip": "65101"
}
],
"party": "Republican Party",
"phones": [
"(573) 751-4936"
],
"urls": [
"https://www.sos.mo.gov/"
],
"emails": [
"info@sos.mo.gov"
],
"channels": [
{
"type": "Twitter",
"id": "MissouriSOS"
}
]
},
{
"name": "Cathy Rinehart",
"address": [
{
"line1": "1 Courthouse Square",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Democratic Party",
"phones": [
"(816) 407-3500"
],
"urls": [
"https://www.claycountymo.gov/departments/assessor"
],
"emails": [
"assessor@claycountymo.gov"
]
},
{
"name": "Ted Graves",
"address": [
{
"line1": "1 Courthouse Square",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3540"
],
"urls": [
"https://www.claycountymo.gov/departments/treasurer"
],
"emails": [
"treasurer@claycountymo.gov"
]
},
{
"name": "Lee Bucksath",
"address": [
{
"line1": "11 South Water Street",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Unknown",
"phones": [
"(816) 407-3900"
],
"urls": [
"http://www.circuit7.net/circuitclerk/"
]
},
{
"name": "Victor S. Hurlbert",
"address": [
{
"line1": "1 Courthouse Square",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3590"
],
"urls": [
"https://www.claycountymo.gov/departments/auditor"
],
"emails": [
"auditor@claycountymo.gov"
]
},
{
"name": "Jerry Nolte",
"address": [
{
"line1": "1 Courthouse Square",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3600"
],
"urls": [
"https://www.claycountymo.gov/departments/boards-commissions"
],
"emails": [
"commission@claycountymo.gov"
]
},
{
"name": "Megan Thompson",
"address": [
{
"line1": "1 Courthouse Square",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3570"
],
"urls": [
"https://www.claycountymo.gov/departments/county-clerk"
],
"emails": [
"clerk@claycountymo.gov"
]
},
{
"name": "Lydia McEvoy",
"address": [
{
"line1": "1 Courthouse Square",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3200"
],
"emails": [
"collector@claycountymo.gov"
]
},
{
"name": "Dan White",
"address": [
{
"line1": "11 S Water Street",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Democratic Party",
"phones": [
"(816) 736-8300"
],
"urls": [
"http://www.circuit7.net/prosatty/"
],
"emails": [
"jim_roberts@claycopa.com"
]
},
{
"name": "Paul Vescovo, III",
"address": [
{
"line1": "12 S. Water St.",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3750"
],
"urls": [
"http://www.claymosheriff.org/index.html"
],
"emails": [
"sheriff@sheriffclayco.com"
]
},
{
"name": "Katee Porter",
"address": [
{
"line1": "1 Courthouse Square",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3550"
],
"urls": [
"https://www.claycountymo.gov/departments/recorder-deeds"
],
"emails": [
"recorder@claycountymo.gov"
]
},
{
"name": "Sarah Mills",
"address": [
{
"line1": "11 S Water Street",
"city": "Liberty",
"state": "MO",
"zip": "64068"
}
],
"party": "Republican Party",
"phones": [
"(816) 407-3250"
],
"urls": [
"http://www.claycomopa.com/"
],
"emails": [
"ccadmin@claycountymo.gov"
]
}
]
}
答案 0 :(得分:1)
您在这里。使用名为 CivicInfo
的类,将其添加到您的项目中。
如果您正在读取文件或流,请打开文件或流(提供示例)或直接将JSON对象(字符串)传递给JsonConvert.DeserializeObject()
方法:
如果您是从现有Stream(即从Web)中读取内容,请仅使用StreamReader
。
Dim civicInfo As CivicInfo.RootObject = Nothing
Using stream As Stream = New FileStream("[File Name]", FileMode.Open, FileAccess.Read, FileShare.Read)
Using sr As StreamReader = New StreamReader(stream)
Dim JSONObject As String = sr.ReadToEnd()
civicInfo = JsonConvert.DeserializeObject(Of CivicInfo.RootObject)(JSONObject)
End Using
End Using
现在您已经有了一个完整的表示JSON的类结构。
您可以照常获取/设置属性值。
例如,使用部分名称提取有关官员的信息:
Dim official = civicInfo.officials.FirstOrDefault(Function(off) off.Name.Contains("Fitzpatrick"))
会返回:
official.Name = "Scott Fitzpatrick"
official.Party = "Republican Party"
'(...)
提取所有民主党官员:
Dim demOfficials = civicInfo.officials.Where(Function(ofs) ofs.Party.Contains("Democratic"))
将返回3个 Official
类的IEnumerable。
提取地区:
Dim district = civicInfo.divisions.FirstOrDefault(Function(dict) dict.Key.Contains("/cd")).Value
会返回:
district.Name = "Missouri's 6th congressional district"
district.OfficeIndices = [List(Of Long)] { 3 }
完成CivicInfo
类:
Imports Newtonsoft.Json
Public Class CivicInfo
Partial Public Class RootObject
Public Property kind As String
Public Property normalizedInput As NormalizedInput
Public Property divisions As Dictionary(Of String, Division)
Public Property offices As List(Of Office)
Public Property officials As List(Of Official)
End Class
Partial Public Class Division
<JsonProperty("name")>
Public Property Name As String
<JsonProperty("officeIndices")>
Public Property OfficeIndices As List(Of Long)
End Class
Partial Public Class NormalizedInput
Public Property Line1 As String
Public Property Line2 As String
Public Property City As String
Public Property State As String
Public Property Zip As String
End Class
Partial Public Class Office
Public Property Name As String
Public Property DivisionId As String
Public Property Levels As List(Of String)
Public Property Roles As List(Of String)
Public Property OfficialIndices As List(Of Long)
End Class
Partial Public Class Official
Public Property Name As String
Public Property Address As List(Of NormalizedInput)
Public Property Party As String
Public Property Phones As List(Of String)
Public Property Urls As List(Of Uri)
Public Property PhotoUrl As Uri
Public Property Channels As List(Of Channel)
Public Property Emails As List(Of String)
End Class
Partial Public Class Channel
Public Property Type As String
Public Property Id As String
End Class
End Class