如何在 C# 对象中解析 {"Europe":{"France":"Paris","UK":"London","Germany":"Berlin" }} Json 键是实际对象值的对象?
How to parse in C# object the {"Europe":{"France":"Paris","UK":"London","Germany":"Berlin"}} Json object where the keys are actual object values?
更新:我想我必须重新表述这个问题:我正在从数据库源获取大陆、国家和首都列表,我必须构建一个具有给定结构的 JSON 对象.
我必须创建一个代表以下 JSON 对象格式的 Dto 对象:
{
"Europe":{
"France":"Paris",
"UK":"London",
"Germany":"Berlin"
}
}
其中“欧洲”是大陆类型对象的值,“法国”、“英国”和“伦敦”是对象国家/地区的值:
public class Country
{
public string Name { get; set; }
public string Capital { get; set; }
}
如何用对象 类 表示 JSON 对象?
试试这个
var json="{\"Europe\":{\"France\":\"Paris\",\"UK\":\"London\",\"Germany\":\"Berlin\"}}";
var result= JsonConvert.DeserializeObject<EuropeCountry>));
var continent = new Continent {
Name = nameof( result.Europe),
Countries = result.Europe.Select(e => new Country {Name=e.Key, Capital=e.Value} ).ToList()
};
public class Continent
{
public string Name { get; set; }
public List<Country> Countries { get; set; }
}
public class EuropeCountries
{
public Dictionary<string, string> Europe {get;set;}
}
按照@的建议使用代理Dictionary<string, Dictionary<string, string>>
卡米洛-特雷文托
using System.Text.Json.Serialization;
using System.Text.Json;
public class Continent
{
public string Name { get; set; }
public List<Country> Countries { get; set; } = new List<Country>();
}
public class Country
{
public string Name { get; set; }
public string Capital { get; set; }
}
string json = @"
{
""Europe"":{
""France"":""Paris"",
""UK"":""London"",
""Germany"":""Berlin""
}
}
";
var dic = JsonSerializer.Deserialize<Dictionary<string,Dictionary<string,string>>>(json);
var continents = new List<Continent>();
foreach(var key in dic.Keys) {
var continent = new Continent() { Name = key };
foreach(var subkey in dic[key].Keys)
{
continent.Countries.Add(new Country() { Name = subkey, Capital = dic[key][subkey] });
}
continents.Add(continent);
}
更新:我想我必须重新表述这个问题:我正在从数据库源获取大陆、国家和首都列表,我必须构建一个具有给定结构的 JSON 对象.
我必须创建一个代表以下 JSON 对象格式的 Dto 对象:
{
"Europe":{
"France":"Paris",
"UK":"London",
"Germany":"Berlin"
}
}
其中“欧洲”是大陆类型对象的值,“法国”、“英国”和“伦敦”是对象国家/地区的值:
public class Country
{
public string Name { get; set; }
public string Capital { get; set; }
}
如何用对象 类 表示 JSON 对象?
试试这个
var json="{\"Europe\":{\"France\":\"Paris\",\"UK\":\"London\",\"Germany\":\"Berlin\"}}";
var result= JsonConvert.DeserializeObject<EuropeCountry>));
var continent = new Continent {
Name = nameof( result.Europe),
Countries = result.Europe.Select(e => new Country {Name=e.Key, Capital=e.Value} ).ToList()
};
public class Continent
{
public string Name { get; set; }
public List<Country> Countries { get; set; }
}
public class EuropeCountries
{
public Dictionary<string, string> Europe {get;set;}
}
按照@的建议使用代理Dictionary<string, Dictionary<string, string>>
卡米洛-特雷文托
using System.Text.Json.Serialization;
using System.Text.Json;
public class Continent
{
public string Name { get; set; }
public List<Country> Countries { get; set; } = new List<Country>();
}
public class Country
{
public string Name { get; set; }
public string Capital { get; set; }
}
string json = @"
{
""Europe"":{
""France"":""Paris"",
""UK"":""London"",
""Germany"":""Berlin""
}
}
";
var dic = JsonSerializer.Deserialize<Dictionary<string,Dictionary<string,string>>>(json);
var continents = new List<Continent>();
foreach(var key in dic.Keys) {
var continent = new Continent() { Name = key };
foreach(var subkey in dic[key].Keys)
{
continent.Countries.Add(new Country() { Name = subkey, Capital = dic[key][subkey] });
}
continents.Add(continent);
}