C# 从IEnumerable foreach循环获取值
我需要从一个JSON文件创建一个c对象,并且需要以下工作解决方案:C# 从IEnumerable foreach循环获取值,c#,.net,json,json.net,C#,.net,Json,Json.net,我需要从一个JSON文件创建一个c对象,并且需要以下工作解决方案: var deserialized = JsonConvert.DeserializeObject<JObject>(json); var locations = ( from state in deserialized.Properties().Select(v => v.Value).OfType<JObject>() from city in state.Properties()
var deserialized = JsonConvert.DeserializeObject<JObject>(json);
var locations = (
from state in deserialized.Properties().Select(v => v.Value).OfType<JObject>()
from city in state.Properties().Select(v => v.Value).OfType<JArray>()
from location in city
select new Location
{
Name = location.Value<string>("Name"),
Address = location.Value<string>("Address"),
City = location.Value<string>("City"),
State = location.Value<string>("State"),
Zip = location.Value<string>("Zip")
}).ToList();
JSON:
代码:
我试图通过使用IEnumerable来解决这个限制,但我不确定如何获取所需的值的语法
string json = File.ReadAllText(@"C:json.txt");
var deserialisedJson = (IEnumerable)JsonConvert.DeserializeObject(json);
var locations = new List<Location>();
foreach (var root in deserialisedJson)
{
foreach (var state in (IEnumerable)root)
{
foreach (var city in (IEnumerable)state)
{
foreach (var location in (IEnumerable)city)
{
Location loc = new Location();
loc.Name = //What goes here???
loc.Address = //What goes here???
loc.City = //What goes here???
loc.State = //What goes here???
loc.Zip = //What goes here???
locations.Add(loc);
}
}
}
}
此选项用于选择所需的所有位置对象:
var deserialisedJson = (IEnumerable)JsonConvert.DeserializeObject(json);
JObject jObj = JObject.Parse(json);
//Get all tokens that are under AK/(some descendant)/all items from collection
var result = jObj.SelectTokens("AK.*.[*]")
.Select(x => new Location
{
Name = x["Name"].Value<string>(),
Address = x["Address"].Value<string>(),
City = x["City"].Value<string>(),
State = x["State"].Value<string>(),
Zip = x["Zip"].Value<string>(),
}).ToList();
在.Net 3.5项目中进行了测试,并使用了适用于.Net的Newtonsoft.Json包。以下是一个快速解决方案:
var deserialized = JsonConvert.DeserializeObject<JObject>(json);
var locations = (
from state in deserialized.Properties().Select(v => v.Value).OfType<JObject>()
from city in state.Properties().Select(v => v.Value).OfType<JArray>()
from location in city
select new Location
{
Name = location.Value<string>("Name"),
Address = location.Value<string>("Address"),
City = location.Value<string>("City"),
State = location.Value<string>("State"),
Zip = location.Value<string>("Zip")
}).ToList();
您可以使用CLASE让newtonsoft解决您需要的问题
public class Location
{
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class AK
{
public Location[] Anchorage { get; set; }
public Location[] Fairbanks { get; set; }
}
var ak = JsonConvert.DeserializeObject<AK>(json);
location[Name]?添加一个小的json示例Using location[Name]给出一个错误:无法使用[]将索引应用于“object”类型的表达式,而不是动态反序列化json=JsonConvert.DeserializeObjectjson;ClassThatHasJsonStructure反序列化Json=Json.Decodejson是否在.Net3.5中工作?您实际上不需要类型。您可以动态地反序列化它。但是我们需要json的结构。@pixelpul-这对你有帮助吗?如果你在同一个列表中需要它们,只需将它们浓缩就行了。var位置=ak.Anchorage.Concatak.Fairbanks;
var deserialized = JsonConvert.DeserializeObject<JObject>(json);
var locations = (
from state in deserialized.Properties().Select(v => v.Value).OfType<JObject>()
from city in state.Properties().Select(v => v.Value).OfType<JArray>()
from location in city
select new Location
{
Name = location.Value<string>("Name"),
Address = location.Value<string>("Address"),
City = location.Value<string>("City"),
State = location.Value<string>("State"),
Zip = location.Value<string>("Zip")
}).ToList();
public class Location
{
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class AK
{
public Location[] Anchorage { get; set; }
public Location[] Fairbanks { get; set; }
}
var ak = JsonConvert.DeserializeObject<AK>(json);