Home > database >  How is such a request interpreted in the code?
How is such a request interpreted in the code?

Time:01-04

I have a JSON request like this:

{
  "locations" : [
    "53.44059300,-2.22992800",
    "53.36246000,-2.26683200"
  ],
}

How I got it from Postmana:

var body = @"{
                "   "\n"  
                            @"  ""locations"" : [
                "   "\n"  
                            @"    ""53.44059300,-2.22992800"",
                "   "\n"  
                            @"    ""53.36246000,-2.26683200""
                "   "\n"  
                            @"  ]
                "   "\n"    @"}";

And now I wanted to rewrite the string query into a structured one:

var request = new Request();
            foreach (var address in incomingRequest.Addresses)
            {
                request .Locations.Add(new Locations
                {
                    Latitude = address.Latitude,
                    Longitude = address.Longitude
                });
            }

Request class looks like this:

internal class Request : DerivedA
    {
        public List<Locations> Locations { get; set; } = new List<Locations>();
    }

But, in the end, my output is different from the initial request:

{
   "Locations":[
      {
         "Latitude":51.469575800000,
         "Longitude":-0.449607200000
      },
      {
         "Latitude":53.361936300000,
         "Longitude":-2.272971700000
      }
   ]
}

CodePudding user response:

This is just a string:

"53.44059300,-2.22992800"

So locations would be just an array of strings:

internal class Request : DerivedA
{
    public List<string> Locations { get; set; } = new List<string>();
}

Which you'd populate with strings:

var request = new Request();
foreach (var address in incomingRequest.Addresses)
{
    request.Locations.Add($"{address.Latitude},{address.Longitude}");
}

As an aside...

Naming is important. You currently have (but may no longer need) a class called Locations which represents a single location. Mixing up plurality is a bug waiting to happen.

  •  Tags:  
  • c#
  • Related