0

我有这个 JSON(如下),我无法选择“MARYLAND”、“NEW YORK”、“PENNSYLVANIA”的字符串列表。

    {
  "displayFieldName": "NAME",
  "fieldAliases": {
    "STATE": "STATE"
  },
  "fields": [
    {
      "name": "STATE",
      "type": "esriFieldTypeString",
      "alias": "STATE",
      "length": 20
    }
  ],
  "features": [
    {
      "attributes": {
        "STATE": "Maryland"
      }
    },
    {
      "attributes": {
        "STATE": "New York"
      }
    },
    {
      "attributes": {
        "STATE": "Pennsylvania"
      }
    }
  ]
}

到目前为止,我正在获取 json 字符串并将其反序列化为 JObject,我可以看到孩子们。不过,我在进一步使用它时遇到了麻烦,并且它不适合我见过的其他示例,因为“功能”是“属性”的集合。我在编写 linq 以进入下一个级别时遇到问题。

这是我的代码:

            var foo = response.Content.ReadAsStringAsync().Result;

            var json = (JObject)JsonConvert.DeserializeObject(foo);

            var cf = json["features"].Children();

任何人都可以帮助我使用 linq 语句从中获取状态字符串吗?

谢谢

4

1 回答 1

0

假设您的JObject课程看起来像下面的示例,您可以执行以下操作:

string[] states = json.features.SelectMany(f => f.attributes).ToArray();

这将产生一个包含 Maryland、New York 和 Pennsylvania 三个条目的单个数组。

完整样本:

class JObject
{
    public Feature[] Features { get; set; }
}

class Feature
{
    public string[] Attributes { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Feature f1 = new Feature { Attributes = new[] { "Maryland" } };
        Feature f2 = new Feature { Attributes = new[] { "New York" } };
        Feature f3 = new Feature { Attributes = new[] { "Pennsylvania" } };

        JObject state = new JObject
        {
            Features = new[] { f1, f2, f3 }
        };

        string[] states = state.Features.SelectMany(f => f.Attributes).ToArray();
    }
}
于 2013-06-29T15:48:43.510 回答