How to loop through the JSON Array and convert it to 2D array using C#?

Umme Athiya 21 Reputation points
2022-06-07T17:38:16.843+00:00

I have a sample json file, how to loop through it and push it to a 2D array.

 {
  "ThermalData": [
    {
      "X": 0,
      "Z": 0,
      "TemperatureValue": 27.638069152832031,
      "RawValue": 9020.0
    },
    {
      "X": 1,
      "Z": 0,
      "TemperatureValue": 27.638069152832031,
      "RawValue": 9020.0
    },
    {
      "X": 2,
      "Z": 0,
      "TemperatureValue": 27.638069152832031,
      "RawValue": 9020.0
    },
    {
      "X": 3,
      "Z": 0,
      "TemperatureValue": 27.638069152832031,
      "RawValue": 9020.0
    },
    {
      "X": 4,
      "Z": 0,
      "TemperatureValue": 27.470109939575195,
      "RawValue": 9009.0
    },
    {
      "X": 5,
      "Z": 0,
      "TemperatureValue": 27.4548282623291,
      "RawValue": 9008.0
    },
    {
      "X": 6,
      "Z": 0,
      "TemperatureValue": 27.439544677734375,
      "RawValue": 9007.0
    },
    {
      "X": 7,
      "Z": 0,
      "TemperatureValue": 27.592287063598633,
      "RawValue": 9017.0
    },
    {
      "X": 8,
      "Z": 0,
      "TemperatureValue": 27.592287063598633,
      "RawValue": 9017.0
    },
    {
      "X": 9,
      "Z": 0,
      "TemperatureValue": 27.424259185791016,
      "RawValue": 9006.0
    },
    {
      "X": 10,
      "Z": 0,
      "TemperatureValue": 27.408969879150391,
      "RawValue": 9005.0
    },
    {
      "X": 11,
      "Z": 0,
      "TemperatureValue": 27.393680572509766,
      "RawValue": 9004.0
    },
    {
      "X": 12,
      "Z": 0,
      "TemperatureValue": 27.378387451171875,
      "RawValue": 9003.0
    },
    {
      "X": 13,
      "Z": 0,
      "TemperatureValue": 27.363092422485352,
      "RawValue": 9002.0
    },
    {
      "X": 14,
      "Z": 0,
      "TemperatureValue": 27.347795486450195,
      "RawValue": 9001.0
    },
    {
      "X": 15,
      "Z": 0,
      "TemperatureValue": 27.332496643066406,
      "RawValue": 9000.0
    },
    {
      "X": 16,
      "Z": 0,
      "TemperatureValue": 27.317195892333984,
      "RawValue": 8999.0
    },
    {
      "X": 17,
      "Z": 0,
      "TemperatureValue": 27.30189323425293,
      "RawValue": 8998.0
    }
   ]
  }

Note: - Here X and Y values are width and height of the image respectively (640*480). One more thing I need it in integer array. So I need to convert JSON Array to a 2 dimensional array. If anyone has come across this kindly let me know.

int[,] arr = new int[2,2];

Developer technologies C#
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2022-06-07T19:03:07.577+00:00

    Try one of solutions:

    string s = . . . json string . . .
    
    var d = new { ThermalData = new[ ] { new { X = 0, Y = 0 } } };
    
    d = JsonSerializer.Deserialize( s, d.GetType( ) ) as dynamic;
    
    int[,] arr = new int[d.ThermalData.Length, 2];
    
    for( int i = 0; i < d.ThermalData.Length; ++i )
    {
        arr[i, 0] = d.ThermalData[i].X;
        arr[i, 1] = d.ThermalData[i].Y;
    }
    

    Use the "Manage NuGet Packages" command to add the standard System.Text.Json package (if not included yet).

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-06-07T20:30:02.253+00:00

    assuming you deserialized the json to an object named data:

    var a = data.ThermalData.Select(r => new [] {r.X,r.Z}).ToArray();
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.