question

moondaddy-8531 avatar image
0 Votes"
moondaddy-8531 asked Viorel-1 edited

Newtonsoft.Json: Need help parsing RestSharp.IRestResponse.Content into a list

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:

81330-addressjson.png

I need to convert this to a list that contains only:
1. address
2. location
3. score
4. ResultID


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": ""
       }
     },


dotnet-csharp
addressjson.png (14.2 KiB)
addressjson.png (14.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 edited

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 . . .
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.