I have a process that parses through similar but different json objects from different companies. For example:
COMPANY A Json looks like this:
{
"str": "502 Oak Lane",
"city": "Los Angeles",
"st": "CA",
"zip": "91403"
}
COMPANY B Json looks like this:
{
"streetaddress": "999 Orange Ave",
"cityname": "Los Angeles",
"statename": "CA",
"zipcode": "92039"
}
COMPANY C Json looks like this:
{
"locations": [
{
"streetaddress": [
"52 Pine Street"
],
"sublocality1": "Los Angeles",
"sublocality2": "CA",
"postal_code": "91403"
},
{
"streetaddress": [
"252 Main St"
],
"sublocality1": "Los Angeles",
"sublocality2": "CA",
"postal_code": "91403"
}
]
}
I also have Company D, E, F... and everybody formats their json a bit differently. I'm trying to write a generic parser to go through and get the 'city' from each of these objects. It's easy enough to do for Company A or company B.
string citynode = "$.city"; //ex. for Company A
string GetCity(jsoninputtext, citynode);
...
string citynode = "$.cityname"; //ex. for Company B
string GetCity(jsoninputtext, citynode);
...
function GetCity(string jsoninputtext, string citynode)
{
var jsonDynamic = JsonConvert.DeserializeObject<dynamic>(jsoninputtext);
string city = (string)jsonDynamic.SelectToken(citynode).Value;
return city;
}
However, I'm stuck with objects like Company C which have multiple locations or that are children of other nodes ('locations'). All I need to get is the FIRST 'city', I don't care about any subsequent ones but I don't know how to do this with my generic GetCity() function. Any help would be greatly appreciated.