我有一个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”,“安克雷奇”,“费尔班克斯”没有通用名称时,我不确定如何获取内在值(名称,地址,城市,州,邮编)?
答案 0 :(得分:2)
使用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; }
}
从here修改的示例。
快速更新,我重新阅读了这个问题,并发现你在迭代这个对象时遇到了困难。错过了第一轮,你走了:
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);
}
}
}
答案 1 :(得分:0)
- 转到json2csharp.com
过去你的JSON in the Box。
Clik on Generate。
您将获得对象模型的C#代码
通过var model = JsonConvert.DeserializeObject(json)反序列化;使用NewtonJson
答案 2 :(得分:0)
请看一下这个问题:
Json.net use the JsonProperty to get the inherited property
编写一个继承自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());
答案 3 :(得分:0)
var JsonResult = JsonConvert.SerializeObject(Account);
var obj = JsonConvert.DeserializeObject(JsonResult);
答案 4 :(得分:-1)
&#34;安克雷奇&#34;,&#34;费尔班克斯&#34;等不应该是&#34;属性名称&#34;而是像
一样在对象内部{
"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。