Answer the question
In order to leave comments, you need to log in
How to deserialize json and object value to insert into ComboBox?
Hello. I apologize in advance for the stupid question, I'm new to C#. I have a json file like this:
{
"name": "SiO2",
"h": 906,
"s": -477,
"a": 44,
"b": 67,
"c": 47
},
{
"name": "Al2O3",
"h": 805,
"s": -155,
"a": 53,
"b": 73,
"c": 62
},
...
public class CompoundProps
{
public string name { set; get; }
public float h { set; get; }
public float s { set; get; }
public float a { set; get; }
public float b { set; get; }
public float c { set; get; }
}
using (FileStream fs = new FileStream("db1.json", FileMode.OpenOrCreate))
{
CompoundProps props = JsonSerializer.Deserialize<CompoundProps>(fs);
Console.WriteLine($"Name: {props.name} H: {props.h} S: {props.s} a: {props.a} b: {props.b} c: {props.c}");
}
Answer the question
In order to leave comments, you need to log in
Your code is not valid
[{
"name": "SiO2",
"h": 906,
"s": -477,
"a": 44,
"b": 67,
"c": 47
},
{
"name": "Al2O3",
"h": 805,
"s": -155,
"a": 53,
"b": 73,
"c": 62
}
]
using (FileStream fs = new FileStream("db1.json", FileMode.OpenOrCreate))
{
CompoundProps propss = JsonSerializer.Deserialize<CompoundProps[]>(fs.ReadToEnd());
var props = propss[0];
Console.WriteLine($"Name: {props.name} H: {props.h} S: {props.s} a: {props.a} b: {props.b} c: {props.c}");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question