gpt4 book ai didi

c# - 属性名称不同时的 LINQ 和 JSON.NET

转载 作者:太空狗 更新时间:2023-10-29 17:50:10 25 4
gpt4 key购买 nike

我正在尝试将一些 JSON 内容解析为 C#。对于更简单的情况,我在 JSON.NET 方面取得了巨大成功,并且非常感谢 LINQ 提供程序提供的简洁方法。这是一个示例,其中我正在下载有关 map 中图层的信息并填充名为(令人惊讶!)图层的类的一些属性:

        using (var client = new WebClient())
{
_content = client.DownloadString(_url.AbsoluteUri + OutputFormats.Json);
}

JObject json = JObject.Parse(_content);
IEnumerable<Field> fields = from f in json["fields"].Children()
select new Field(
(string)f["name"],
(string)f["alias"],
(EsriFieldType)Enum.Parse(typeof(EsriFieldType), (string)f["type"])
);
_fields = fields.ToList();
_displayFieldName = (string)json["displayField"];

您可以查看此网址以获取该方法的 JSON 的详细信息:http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/WaterTemplate/WaterDistributionNetwork/MapServer/1?f=json&pretty=true .但是,当我需要将与 map 层关联的各个数据字段转换为 DataTable 或什至只是一个字典结构时,问题就来了。问题在于,与 RSS 提要或其他一致的格式不同,字段名称和字段数量会随着 map 层的不同而变化。这是我运行查询的示例:

    [Test]
[Category(Online)]
public void Can_query_a_single_feature_by_id()
{
var layer = _map.LayersWithName(ObjectMother.LayerWithoutOID)[0];
layer.FindFeatureById("13141");
Assert.IsNotNull(layer.QueryResults);
}

在 layer.FindFeatureById 中运行的代码是这样的,包括我卡住的部分:

        public void FindFeatureById(string id)
{
var queryThis = ObjectIdField() ?? DisplayField();
var queryUrl = string.Format("/query{0}&outFields=*&where=", OutputFormats.Json);
var whereClause = queryThis.DataType == typeof(string)
? string.Format("{0}='{1}'", queryThis.Name, id)
: string.Format("{0}={1}", queryThis.Name, id);
var where = queryUrl + HttpUtility.UrlEncode(whereClause);
var url = new Uri(_url.AbsoluteUri + where);
Debug.WriteLine(url.AbsoluteUri);
string result;

using (var client = new WebClient())
{
result = client.DownloadString(url);
}

JObject json = JObject.Parse(result);
IEnumerable<string> fields = from r in json["fieldAliases"].Children()
select ((JProperty)r).Name;
// Erm...not sure how to get this done.
// Basically need to populate class instances/rows with the
// values for each field where the list of fields is not
// known beforehand.

}

您可以通过访问此 URL 看到 JSON 吐出(注意剪切和粘贴时的编码):href="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/WaterTemplate/WaterDistributionNetwork/MapServer/1/query?f=json&outFields=*&where=FACILITYID%3d'13141'

所以我的问题(终于!)是这样的。如何循环遍历“特征”中的“属性”集合以获取实际字段值。您可以看到我已经弄清楚如何从 fieldAliases 中获取字段名称,但之后我就被难住了。我一直在一个看起来像这样的文件上修改 JsonReader,但仍然没有快乐:

{
"displayFieldName" : "FACILITYID",
"fieldAliases" : {
"FACILITYID" : "Facility Identifier",
"ACCOUNTID" : "Account Identifier",
"LOCATIONID" : "Location Identifier",
"CRITICAL" : "Critical Customer",
"ENABLED" : "Enabled",
"ACTIVEFLAG" : "Active Flag",
"OWNEDBY" : "Owned By",
"MAINTBY" : "Managed By"
},
"features" : [
{
"attributes" : {
"FACILITYID" : "3689",
"ACCOUNTID" : "12425",
"LOCATIONID" : "12425",
"CRITICAL" : 1,
"ENABLED" : 1,
"ACTIVEFLAG" : 1,
"OWNEDBY" : 1,
"MAINTBY" : 1
}
},
{
"attributes" : {
"FACILITYID" : "4222",
"ACCOUNTID" : "12958",
"LOCATIONID" : "12958",
"CRITICAL" : 1,
"ENABLED" : 1,
"ACTIVEFLAG" : 1,
"OWNEDBY" : 1,
"MAINTBY" : 1
}
}
]
}

最佳答案

要获取属性和值的快速而粗略的(非 LINQ)方法,请尝试以下操作:

JObject jo = JObject.Parse(json);

foreach (JObject j in jo["features"])
{
foreach (JProperty k in j["attributes"])
{
Console.WriteLine(k.Name + " = " + k.Value);
}
}

这并不理想,但当您不知道将要返回的字段名称时它会起作用。如果我找到更好的方法,我会更新它。

关于c# - 属性名称不同时的 LINQ 和 JSON.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1403242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com