2

In my c# project, I would like to access specific information (POI name and distance) inside a complex and nested JSON.

This JSON is the result of an Azure Maps API call.

I have tried to deserialise it into an object. But this JSON is too complex and I am unable to do it.

What is the best way to extract the information I need ?

{
  "summary": {
    "query": "university",
    "queryType": "NON_NEAR",
    "queryTime": 103,
    "numResults": 1,
    "offset": 0,
    "totalResults": 216684,
    "fuzzyLevel": 1,
    "geoBias": {
      "lat": 48.008446,
      "lon": 7.821583
    }
  },
  "results": [
    {
      "type": "POI",
      "id": "DE/POI/p0/1505647",
      "score": 2.574,
      "dist": 774.6544330765787,
      "info": "search:ta:276009006412786-DE",
      "poi": {
        "name": "Universität Freiburg Medizinische Fakultät",
        "phone": "+(49)-(761)-27072350",
        "url": "www.med.uni-freiburg.de",
        "categories": [
          "college/university"
        ],
        "classifications": [
          {
            "code": "COLLEGE_UNIVERSITY",
            "names": [
              {
                "nameLocale": "en-US",
                "name": "college/university"
              }
            ]
          }
        ]
      },
      "address": {
        "streetName": "Elsässer Straße",
        "municipalitySubdivision": "Mooswald",
        "municipality": "Freiburg im Breisgau",
        "countrySecondarySubdivision": "Freiburg im Breisgau",
        "countrySubdivision": "Baden-Württemberg",
        "postalCode": "79110",
        "countryCode": "DE",
        "country": "Germany",
        "countryCodeISO3": "DEU",
        "freeformAddress": "Elsässer Straße, 79110 Freiburg Im Breisgau"
      },
      "position": {
        "lat": 48.00894,
        "lon": 7.83197
      },
      "viewport": {
        "topLeftPoint": {
          "lat": 48.00984,
          "lon": 7.83063
        },
        "btmRightPoint": {
          "lat": 48.00804,
          "lon": 7.83331
        }
      },
      "entryPoints": [
        {
          "type": "main",
          "position": {
            "lat": 48.00931,
            "lon": 7.83259
          }
        }
      ]
    }
  ]
}
rbrundritt
  • 16,570
  • 2
  • 21
  • 46
Farjad
  • 67
  • 1
  • 7
  • 1
    Hello and welcome to Stack Overflow. Please take a moment to review the following how-to resources: [How to Ask](https://stackoverflow.com/help/how-to-ask) and [Complete Examples](https://stackoverflow.com/help/mcve). What have you tried so far? – Pedro Rodrigues Mar 27 '19 at 18:08
  • Possible duplicate of [How could I call Azure Maps API in C# with authorisation and client id?](https://stackoverflow.com/questions/55367075/how-could-i-call-azure-maps-api-in-c-sharp-with-authorisation-and-client-id) – rbrundritt Apr 04 '19 at 20:06
  • There is an open source project that provides a .NET client for the Azure Maps REST services. There is a NuGet package too. You can find it here: https://github.com/perfahlen/AzureMapsRestServices The Azure Maps plans on also providing an official .NET client for the rest services later this year. – rbrundritt May 20 '19 at 17:21

3 Answers3

2

i converted your json via json2csharp then you can use Newtonsoft to deserialize them

RootObject root= JsonConvert.DeserializeObject(JSONstring)

   public class GeoBias
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class Summary
    {
        public string query { get; set; }
        public string queryType { get; set; }
        public int queryTime { get; set; }
        public int numResults { get; set; }
        public int offset { get; set; }
        public int totalResults { get; set; }
        public int fuzzyLevel { get; set; }
        public GeoBias geoBias { get; set; }
    }

    public class Name
    {
        public string nameLocale { get; set; }
        public string name { get; set; }
    }

    public class Classification
    {
        public string code { get; set; }
        public List<Name> names { get; set; }
    }

    public class Poi
    {
        public string name { get; set; }
        public string phone { get; set; }
        public string url { get; set; }
        public List<string> categories { get; set; }
        public List<Classification> classifications { get; set; }
    }

    public class Address
    {
        public string streetName { get; set; }
        public string municipalitySubdivision { get; set; }
        public string municipality { get; set; }
        public string countrySecondarySubdivision { get; set; }
        public string countrySubdivision { get; set; }
        public string postalCode { get; set; }
        public string countryCode { get; set; }
        public string country { get; set; }
        public string countryCodeISO3 { get; set; }
        public string freeformAddress { get; set; }
    }

    public class Position
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class TopLeftPoint
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class BtmRightPoint
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class Viewport
    {
        public TopLeftPoint topLeftPoint { get; set; }
        public BtmRightPoint btmRightPoint { get; set; }
    }

    public class Position2
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class EntryPoint
    {
        public string type { get; set; }
        public Position2 position { get; set; }
    }

    public class Result
    {
        public string type { get; set; }
        public string id { get; set; }
        public double score { get; set; }
        public double dist { get; set; }
        public string info { get; set; }
        public Poi poi { get; set; }
        public Address address { get; set; }
        public Position position { get; set; }
        public Viewport viewport { get; set; }
        public List<EntryPoint> entryPoints { get; set; }
    }

    public class RootObject
    {
        public Summary summary { get; set; }
        public List<Result> results { get; set; }
    }
1

Step 1: Parse your JSON in a JSON parser website such as https://jsonparser.org This will help you understand the content and how it will be translated as an object. For example, your JSON string gives this result :

enter image description here

Step2: Open the query tool of this website, this will help you find out the object path to the information you need. For example, for your JSON string, to access the POI name :

enter image description here

Step 3: In your Visual Studio project, install the NuGet package: Newtonsoft.Json and Microsoft.CSharp in your shared library. If you are processing the JSON in a separate library, please also install the Newtonsoft.Json NuGet package in the main project.

Step 4: If JSONstring is your JSON string :

using Newtonsoft.Json;

dynamic NewObject = JsonConvert.DeserializeObject<dynamic>(JSONstring);


string Name = NewObject.results[0].poi.name;

string Distance = NewObject.results[0].dist;
André
  • 108
  • 7
1

You have at least 2 solutions possible:

Either you create classes that mirror the content of the json you are expecting

public class MyJSON
{
  public Summary summary { get; set; }

  public List<Result> results { get; set; }
  ...
}

public class Summary
{
   public string query { get; set; }
   ...
}

Then you could deserialize using Newtonsoft.Json

JsonConvert.DeserializeObject<MyJSON>(jsonstring);

Or you could directly deserialize to a dynamic object and access the properties directly by name.

  dynamic data = JsonConvert.DeserializeObject<dynamic>(jsonstring);      
  string query = data[0].summary.query;

Solution 1 requires you to create the classes first, but is faster and more secure to access (less prone to wrong naming, or data structure changes)

Solution 2 is much more volatile and flexible, you just access what you need. But you could get exceptions if you try to access properties that do not exist in the json object.

Pic Mickael
  • 1,244
  • 19
  • 36