使用Newtonsoft.Json反序列化White Pages Pro API JSON遇字段访问错误
Hey there, let's break down what's going wrong here and fix it step by step.
The core issue is that your jsonPersonComplex class doesn't match the structure of the JSON response from White Pages Pro API, and you're trying to access nested properties as if they're direct properties of the main class. Let's go through the fixes:
1. Fix the Entity Class (jsonPersonComplex)
Your class is missing critical properties that map to key sections of the JSON: current_addresses and associated_people. Additionally, ensure all nested classes align with the JSON structure. Here's the corrected class:
using System; using System.Collections.Generic; using Newtonsoft.Json; namespace WhitePagesTool { class jsonPersonComplex { public string id { get; set; } public string phone_number { get; set; } public bool is_valid { get; set; } public string country_calling_code { get; set; } // Added to match JSON public string line_type { get; set; } public string carrier { get; set; } public bool is_prepaid { get; set; } public bool is_commercial { get; set; } public List<BelongsTo> belongs_to { get; set; } public List<CurrentAddress> current_addresses { get; set; } // Added public List<object> historical_addresses { get; set; } // Added public List<AssociatedPeople> associated_people { get; set; } // Added public List<object> alternate_phones { get; set; } // Added public object error { get; set; } // Added public List<object> warnings { get; set; } // Added public class BelongsTo { public string id { get; set; } // Added to match JSON public string name { get; set; } public string firstname { get; set; } public string middlename { get; set; } public string lastname { get; set; } public string age_range { get; set; } public string gender { get; set; } public string type { get; set; } public string link_to_phone_start_date { get; set; } // Added } public class CurrentAddress { public string id { get; set; } // Added public string location_type { get; set; } // Added public string street_line_1 { get; set; } public object street_line_2 { get; set; } public string city { get; set; } public string postal_code { get; set; } public string zip4 { get; set; } // Added public string state_code { get; set; } public string country_code { get; set; } public LatLong lat_long { get; set; } public bool is_active { get; set; } // Added public string delivery_point { get; set; } // Added public string link_to_person_start_date { get; set; } // Added } public class LatLong { public double latitude { get; set; } public double longitude { get; set; } public string accuracy { get; set; } } public class AssociatedPeople { public string id { get; set; } // Added public string name { get; set; } public string firstname { get; set; } public string middlename { get; set; } public string lastname { get; set; } public string relation { get; set; } } } }
2. Fix How You Access Nested Properties
Fields like name, firstname, and street_line_1 aren't direct properties of jsonPersonComplex—they're nested inside arrays (belongs_to, current_addresses, associated_people). You need to access them by first targeting the array elements (either by index or looping through the array). Here's the corrected deserialiseJSON method:
private void deserialiseJSON(string strJSON) { try { var jPerson = JsonConvert.DeserializeObject<jsonPersonComplex>(strJSON); // Access top-level properties (these worked before) debugOutput("Phone ID: " + jPerson.id); debugOutput("Phone Number: " + jPerson.phone_number); debugOutput("Valid Number: " + jPerson.is_valid); debugOutput("Line Type: " + jPerson.line_type); debugOutput("Carrier: " + jPerson.carrier); debugOutput("Prepaid: " + jPerson.is_prepaid); debugOutput("Commercial: " + jPerson.is_commercial); // Access belongs_to data (nested in array) if (jPerson.belongs_to != null && jPerson.belongs_to.Count > 0) { var primaryPerson = jPerson.belongs_to[0]; debugOutput("--- Primary Person ---"); debugOutput("Name: " + primaryPerson.name); debugOutput("First Name: " + primaryPerson.firstname); debugOutput("Middle Name: " + primaryPerson.middlename); debugOutput("Last Name: " + primaryPerson.lastname); debugOutput("Age Range: " + primaryPerson.age_range); debugOutput("Gender: " + primaryPerson.gender); } // Access current_addresses data (nested in array) if (jPerson.current_addresses != null && jPerson.current_addresses.Count > 0) { var primaryAddress = jPerson.current_addresses[0]; debugOutput("--- Primary Address ---"); debugOutput("Address: " + primaryAddress.street_line_1); debugOutput("Extended Address: " + primaryAddress.street_line_2); debugOutput("City: " + primaryAddress.city); debugOutput("Postal Code: " + primaryAddress.postal_code); debugOutput("State: " + primaryAddress.state_code); debugOutput("Country: " + primaryAddress.country_code); if (primaryAddress.lat_long != null) { debugOutput("Latitude: " + primaryAddress.lat_long.latitude); debugOutput("Longitude: " + primaryAddress.lat_long.longitude); debugOutput("Accuracy: " + primaryAddress.lat_long.accuracy); } } // Access associated_people data (array of multiple entries) if (jPerson.associated_people != null && jPerson.associated_people.Count > 0) { debugOutput("--- Associated People ---"); foreach (var assocPerson in jPerson.associated_people) { debugOutput("Associate Name: " + assocPerson.name); debugOutput("Associate First Name: " + assocPerson.firstname); debugOutput("Associate Middle Name: " + assocPerson.middlename); debugOutput("Associate Last Name: " + assocPerson.lastname); debugOutput("Associate Relationship: " + assocPerson.relation); debugOutput("------------------------"); } } } catch(Exception ex) { debugOutput("We Had A Problem: " + ex.Message.ToString()); } }
Why This Fixes Your Errors
Your original code tried to access properties like jPerson.Name directly, but jsonPersonComplex doesn't have a Name property—name lives inside the belongs_to array items. The same applies to address fields and associated people: they're nested in arrays that your original class didn't define, and you weren't accessing the array elements before trying to read their properties.
By updating the class to match the full JSON structure and accessing nested properties through their parent arrays, you'll resolve all those "does not contain a definition" errors.
内容的提问来源于stack exchange,提问作者KhaosBringer

