将JSON加载到C#对象中
本文关键字:对象 JSON 加载 | 更新日期: 2023-09-27 17:59:25
我有一个JSON文件,需要将其转换为C#对象,然后将其写入SQL数据库。JSON的格式如下:
{
"AK": {
"Anchorage": [{
"Name": "John Doe",
"Address": "123 Main St.",
"City": "Anchorage",
"State": "AK",
"Zip": "12345"
}],
"Fairbanks": [{
"Name": "Sally Smith",
"Address": "987 Main St.",
"City": "Fairbanks",
"State": "AK",
"Zip": "98765"
}]
}
}
我有一个C#类,看起来像这样:
public class Location
{
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
}
public class Locations
{
public List<Location> Location { get; set; }
}
我使用的是Newtonsoft JSON库。当外部值"AK"、"Anchorage"、"Fairbanks"没有通用名称时,我不确定如何获取内部值(Name、Address、City、State、Zip)?
使用NewtonSoft:
Location location = JsonConvert.DeserializeObject<Location>(json);
你的课程是这样的:
public class Location
{
public IList<Address> Addresses { get; set; }
}
public class Address {
public string AddressName { get; set; }
[JsonProperty("Name")] # You'll need attributes if the dataset has another name than that of the object's property.
public string PersonName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
从这里修改的示例。
快速更新,我重读了这个问题,发现你在迭代对象时也遇到了困难。错过了第一轮,给你:
var locations = new List<Location>();
dynamic deserialisedJson = JsonConvert.DeserializeObject(json);
// E.g., json => List ( "AK": { ... }, ... )
// so we're iterating the items of that "list", e.g., "AK": { ... }, etc.
foreach (var state in deserialisedJson)
{
// e.g., "AK": { ... } => List ( Anchorage: [{ ... }], Fairbanks: [{ ... }] )
// so we're iterating the items of each item, e.g., Anchorage: [{ ... }], etc.
foreach (var addresses in state)
{
// e.g., Anchorage: [{ ... }, { ... }] => List ( { ... }, { ... } )
// because Anchorage, etc., are arrays, we have to iterate their contents too, to get each address object within them (represented as { ... } above:
foreach (var address in addresses) {
Location location = JsonConvert.DeserializeObject<Location>(address);
// do stuff with location, e.g.,
locations.Add(location);
}
}
}
您尝试过json2csharp.com 吗
-
-转到json2csharp.com
-
通过盒子中的JSON。
-
点击生成。
-
您将获得对象模型的C#代码
-
通过var model=JsonConvert.DescializeObject(json)进行反序列化;使用NewtonJson
请看一下这个问题:
Json.net使用JsonProperty获取继承的属性
编写一个转换器,它继承自JsonConverter:
public class LocationConverter: JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Location));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
Location location = jo.ToObject<Location>();
Location.etc = jo.SelectToken("etc.etc").ToObject<type>();
.
.
.
return location;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
有了这个,您可以加载类中引入的属性,而这些属性的引入方式与json文件中的不同
你可以像这样使用它:
Location location = JsonConvert.DeserializeObject<Location>(json, new LocationConverter());
您可以序列化、反序列化并绑定到classobject
var JsonResult=JsonConvert.SerializeObject(帐户);
var obj=JsonConvert.DescializeObject(JsonResult);
"Anchorage"、"Fairbanks"等不应该是"属性名称",而应该像一样位于对象本身内部
{
"AK":
[{
"AddressName": "Anchorage",
"Name": "John Doe",
"Address": "123 Main St.",
"City": "Anchorage",
"State": "AK",
"Zip": "12345"
}, {
"AddressName": "Fairbanks",
"Name": "Sally Smith",
"Address": "987 Main St.",
"City": "Fairbanks",
"State": "AK",
"Zip": "98765"
}]
}
您可以像往常一样简单地对JSON进行迭代。