-2

Hello i have this JSON and i didn't find any examples of read from a file structured like this. How would the code to read from a JSON like this look? Most of the examples i found don t contain any arrays

  {
  "Name": "ngcutcon1",
  "Objects": [
    {
      "Length": 10,
      "Height": 10,
      "Stock": null,
      "Cost": 100
    }
  ],
  "Items": [
    {
      "Length": 3,
      "Height": 7,
      "Demand": 1,
      "DemandMax": 2,
      "Value": 35
    },
    {
      "Length": 8,
      "Height": 2,
      "Demand": 0,
      "DemandMax": 2,
      "Value": 40
    },
    {
      "Length": 10,
      "Height": 2,
      "Demand": 0,
      "DemandMax": 1,
      "Value": 27
    },
    {
      "Length": 5,
      "Height": 4,
      "Demand": 0,
      "DemandMax": 3,
      "Value": 23
    },
    {
      "Length": 2,
      "Height": 9,
      "Demand": 0,
      "DemandMax": 2,
      "Value": 43
    }
  ]
}
Leo
  • 1
  • 1
    https://json2csharp.com/ paste in your JSON - it will generate you some classes – Jamiec May 16 '22 at 08:34
  • @Jamiec the issue i have is that i don t know how to use these classes properly – Leo May 16 '22 at 08:39
  • app.quicktype.io is like a more sophisticated version of JSON2CSharp.. But both sites give usage instructions in the comment header – Caius Jard May 16 '22 at 09:01
  • 1
    *i don t know how to use these classes properly* - to do what? Theyre just classes like any other; you haven't told us anything about how you want to use this data, you only told us that you want to parse it, which we have advised on. Always ask the question you want answering – Caius Jard May 16 '22 at 09:02
  • 1
    e.g. Had you said "I need to sum up the Value ints in the Items array" we could say: `var r = JsonConvert.Deserialize(text); var sum = r.Items.Sum(i => i.Value);` – Caius Jard May 16 '22 at 09:05
  • I managed to use the class to create some list of objects, thank you for help! – Leo May 16 '22 at 10:46

0 Answers0