【发布时间】:2018-10-14 01:20:34
【问题描述】:
我必须处理一个复杂的 JSON 文件,但遇到了一些障碍。
您将在下面找到该类的一小部分摘录,该类是 XML 格式,但我将其转换为 JSON 以便能够更轻松地处理它。
<Selection Category="M43002NN">
<ReferendumOptionIdentifier>foo</ReferendumOptionIdentifier>
<ValidVotes>6162</ValidVotes>
<CountMetric Id="M" Type="LevelDepositList">43002</CountMetric>
<CountMetric Id="S4" Type="SeatsToBeFilled">23</CountMetric>
<CountMetric Id="S5" Type="SubstitutesMax">0</CountMetric>
<CountMetric Id="S9" Type="LinguisticRegime">2</CountMetric>
<CountMetric Id="S10" Type="VotesDeposited">6620</CountMetric>
<CountMetric Id="S11" Type="BlankAndInvalidVotes">458</CountMetric>
<CountMetric Id="S12" Type="Alderman">0</CountMetric>
<CountMetric Id="S14" Type="ValidVote_E5">0</CountMetric>
<CountMetric Id="S15" Type="BlankAndInvalidVotes_E5">0</CountMetric>
</Selection>
在上面的示例中,我试图提取类型为“SeatsToBeFilled”的 CountMetric 的值。
到目前为止,我能够收集结果并隔离正确的 CountMetric,但我似乎无法获得它的价值。
这是我的课:
public class TotalSelectionClass
{
[JsonProperty("@Category")]
public string Category { get; set; }
public int ValidVotes { get; set; }
public List<CountMetricClass> CountMetric { get; set; }
}
这是我使用的 CountMetricClass:
public class CountMetricClass
{
[JsonProperty("@Id")]
public string Id { get; set; }
[JsonProperty("@Type")]
public string Type { get; set; }
}
以下是我用来获取所需 CountMetric 的代码(为了便于阅读,代码略有缩减):
var TotalSeats = Selection[0].CountMetric.Where(x => x.Type == "SeatsToBeFilled").First();
这会将 CountMetric 对象返回给我,但我如何从中提取值?那么在这种情况下,如何从中提取数字 23?
谢谢。
【问题讨论】:
-
如果您尝试以下操作会得到什么?:var TotalSeats = Selection[0].CountMetric.Where(x => x.Type == "SeatsToBeFilled").First().Value
(); -
我收到一条错误消息,指出 CountMetricClass 不包含值的定义。