C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,989 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am successfully calling and API using RestSharp which returns many addresses (1000s) and each address has about 70 attributes.
I need to collect data from only 4 of them.
This is the code where the response is deserialized into json:
IRestResponse response = client.Execute(request);
dynamic responseContent = JsonConvert.DeserializeObject(response.Content);
and this is what it looks like:
I need to convert this to a list that contains only:
How can I do this?
Sample json for one address:
{
"address": "2500 S Linden Rd, Flint, Michigan, 48532",
"location": {
"x": -83.77313303899996,
"y": 42.988088994000066
},
"score": 100,
"attributes": {
"ResultID": 84401,
"Loc_name": "World",
"Status": "M",
"Score": 100,
"Match_addr": "2500 S Linden Rd, Flint, Michigan, 48532",
"LongLabel": "2500 S Linden Rd, Flint, MI, 48532, USA",
"ShortLabel": "2500 S Linden Rd",
"Addr_type": "PointAddress",
"Type": "",
"PlaceName": "",
"Place_addr": "2500 S Linden Rd, Flint, Michigan, 48532",
"Phone": "",
"URL": "",
"Rank": 20,
"AddBldg": "",
"AddNum": "2500",
"AddNumFrom": "",
"AddNumTo": "",
"AddRange": "",
"Side": "",
"StPreDir": "S",
"StPreType": "",
"StName": "Linden",
"StType": "Rd",
"StDir": "",
"BldgType": "",
"BldgName": "",
"LevelType": "",
"LevelName": "",
"UnitType": "",
"UnitName": "",
"SubAddr": "",
"StAddr": "2500 S Linden Rd",
"Block": "",
"Sector": "",
"Nbrhd": "",
"District": "",
"City": "Flint",
"MetroArea": "",
"Subregion": "Genesee County",
"Region": "Michigan",
"RegionAbbr": "MI",
"Territory": "",
"Zone": "",
"Postal": "48532",
"PostalExt": "7074",
"Country": "USA",
"LangCode": "ENG",
"Distance": 0,
"X": -83.77235888679174,
"Y": 42.98811070294497,
"DisplayX": -83.77313303936884,
"DisplayY": 42.98808899381575,
"Xmin": -83.77413303936885,
"Xmax": -83.77213303936884,
"Ymin": 42.98708899381575,
"Ymax": 42.98908899381575,
"ExInfo": ""
}
},
You can define classes that correspond to JSON structure, for example:
struct Location
{
public double X, Y;
}
struct Attributes
{
public int ResultID;
}
class Item
{
public string address;
public Location location;
public int score;
public Attributes attributes;
}
. . .
string example = @"[
{
'address': '2500 S Linden Rd, Flint, Michigan, 48532',
'location': {
'x': -83.77313303899996,
'y': 42.988088994000066
},
'score': 100,
'attributes': {
'ResultID': 84401,
'Loc_name': 'World',
'Status': 'M'
}
},
{
'address': '2501 S Linden Rd, Flint, Michigan, 48532',
'location': {
'x': -84.77313303899996,
'y': 43.988088994000066
},
'score': 101,
'attributes': {
'ResultID': 84401,
'Loc_name': 'World',
'Status': 'X'
}
}
]";
List<Item> items = JsonConvert.DeserializeObject<List<Item>>( example );
If you prefer dynamic data:
dynamic responseContent = JsonConvert.DeserializeObject( example );
foreach( dynamic i in responseContent )
{
string address = i.address;
double x = i.location.x;
double y = i.location.y;
int score = i.score;
int resultID = i.attributes.ResultID;
// . . . add to list . . .
}