I have the following file:
{ "Ti": 12,
"IES": false,
"End": {
"ABC": "test1",
"XYZ": "test2",
"QWE": "test3"
}
and I have the following C# code that is getting these values through a class:
foreach (var prop in _ABC.GetType().GetProperties())
{
_dic.Add(prop.Name, _ABC.GetType().GetProperty(prop.Name).GetValue(_ABC, null).ToString());
}
This does not give me the values/keys of the dictionary within the existing dictionary. The class ABC looks like following:
public class ABC
{
public string Ti { get; set; }
public string IES { get; set; }
public End End { get; set; }
}
public class END
{
public string ABC { get; set; }
public string XYZ { get; set; }
public string QWE { get; set; }
}
How can I get the loop to go further in to access the values of .END
class
You could try deserializing the contents. It's much more robust and less error prone.
--EDITED--
Change some types:
Ti
from string
to int
IES
from string
to bool
End
from END
to Dictionary<string, string>
public class ABC
{
public int Ti { get; set; }
public bool IES { get; set; }
public Dictionary<string, string> End { get; set; }
}
The deserializing code:
using System.Collections.Generic;
using System.Text.Json;
//[...]
var fileContent = @"{ ""Ti"": 12,
""IES"": false,
""End"": {
""ABC"": ""test1"",
""XYZ"": ""test2"",
""QWE"": ""test3""
}
}";
var abc = JsonSerializer.Deserialize<ABC>(fileContent);
Console.WriteLine($"Ti: {abc.Ti}");
Console.WriteLine($"IES: {abc.IES}");
foreach (var key in abc.End.Keys)
{
Console.WriteLine($"End.{key}: {abc.End[key]}");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.