2

I've got a JSON which I post to my WebAPI C# Application. I need to further process this JSON and save data from it into database table. JSON looks like this, and contains latitudes and longitudes:

{
  "lattitudes": [
    48.75608,
    48.75608,
    48.75608,
    48.75608,
    48.75608,
],
  "longitudes": [
    48.75608,
    2.302038,
    48.75608,
    48.75608,
    48.75608,
  ]
}

Note that the JSON contains two arrays of numbers instead of one array of objects each with 2 attributes.

What I would like to do is to write these "lattitudes" and "longitudes" arrays into Lists/Arrays and then foreach them with SQL "INSERT" statement (I know - it's pretty nasty way of doing this, but I couldn't figure out anything smarter).

How can I deserialize this JSON into two List<T> lists for further processing as described?

dbc
  • 104,963
  • 20
  • 228
  • 340
Kamil Turowski
  • 427
  • 1
  • 4
  • 13
  • I'm not entirely sure what your question is. Is it 1) To deserialize the first JSON sample? 2) To translate the first JSON sample into the second? 3) To translate the 1st sample into an array of pairs? 4) To translate the 1st sample into an array of pairs then write SQL queries to insert into a database? Stack overflow convention is to ask [one question per post](https://meta.stackexchange.com/q/222735) so try not to combine unrelated requests. – dbc May 12 '19 at 22:31
  • Exactly the 1) question You have listed. To deserialize first JSON example into TWO lists, 1JSONarray -> List no. 1, 2JSONArray -> List no.2 – Kamil Turowski May 12 '19 at 22:40
  • Assuming the positions of the longitudes and latitudes correlate to each other (i.e., the 2nd latitude is paired with the 2nd longitude), then I don't see why you wouldn't just loop through them matching them up. What's the actual problem you're having? I think you're making life hard for yourself by keeping them in 2 separate lists if you want to insert them into SQL, but either way is doable. – Ben May 12 '19 at 22:47
  • @KamilTurowski - I tried to simplify your question a little, following your comment. Feel free to revert if I oversimplified. – dbc May 12 '19 at 23:17
  • Ben, problem lays in the way microprocessor has to "book" memory for the actual JSON file. I found out the way with two lists is muuuch better for memory performance. I know this JSON is awful to watch, but I prefer that instead of playing with dynamic creating on uC. @dbc, thanks for simplifying, it's perfect now ;) – Kamil Turowski May 13 '19 at 08:38

1 Answers1

2

To deserialize the first JSON sample into an array of latitude/longitude pairs, you can:

  1. Use one of the code-generation tools mentioned in How to auto-generate a C# class file from a JSON object string to generate a corresponding data model. http://json2csharp.com/ is one option.
  2. Use one of the JSON serializers mentioned in How do I turn a C# object into a JSON string in .NET? to deserialize the JSON to your data model. is currently the most popular.
  3. Combine the arrays into pairs using Enumerable.Zip.

Thus, if we define the following data model:

public class RootObject
{
    public List<double> lattitudes { get; set; }
    public List<double> longitudes { get; set; }
}

public class LatLong
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

We can deserialize your JSON as follows:

var root = JsonConvert.DeserializeObject<RootObject>(jsonString);

var array = root.lattitudes
    .Zip(root.longitudes, (lat, @long) => new LatLong { Latitude = lat, Longitude = @long })
    .ToArray();

Demo fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340