【问题标题】:JSON Getting the value of a list based on the parameterJSON 根据参数获取列表的值
【发布时间】: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 不包含值的定义。

标签: c# json linq


【解决方案1】:

这里的答案实际上取决于您的 JSON 的形状,因此,您如何进行 XML -> JSON 转换。您没有提供 JSON 表单,也没有请求帮助转换 XML -> JSON,所以我将根据可用信息进行回答。

按原样,您无法从 CountMetricClass 中获取 CountMetric 值。这是因为 XML 中保存的值(对于 SeatsToBeFilled 看起来像 23)从未读入该对象。要获得价值,您需要

1) 检查您的 XML 转换器是否实际上将 XML 中的值编码为要解析的 JSON

2) 修改您的 CountMetricClass 以便从该值字段中读取。为了便于阅读,我选择将此字段称为MetricValue,但您显然可以选择最适合您的名称。例如,您的新类可能采用以下形式:

public class CountMetricClass
    {
        [JsonProperty("@Id")]
        public string Id { get; set; }

        [JsonProperty("@Type")]
        public string Type { get; set; }   

        [JsonProperty("@MetricValue")]
        public int MetricValue { get; set; }
    }

3) 一旦我们成功将 MetricValue 读入您的 CountMetricClass,我们可以修改您的 linq 表达式以通过访问目标 CountMetric 中的 MetricValue 字段来获取值,例如:

var TotalSeats = Selection[0].CountMetric.Where(x => x.Type == "SeatsToBeFilled").MetricValue;

【讨论】:

  • 感谢@Sirhamy 查看我的问题。我查看了 NewtonSoft.Json 生成的实际 JSON,这就是我得到的: {"@Id":"S4","@Type":"SeatsToBeFilled","#text":"23" }
【解决方案2】:

为可能遇到相同问题的人提供一些后续服务。

根据 SIRHAMY 的建议,我查看了从 XML 转换的实际 JSON。

{"@Id":"S4","@Type":"SeatsToBeFilled","#text":"23"}

按照 SIRHAMY 的建议,我在 CountMetricClass 中创建了一个文本字段

public class CountMetricClass
        {
            [JsonProperty("@Id")]
            public string Id { get; set; }

            [JsonProperty("@Type")]
            public string Type { get; set; }

            [JsonProperty("#Text")]
            public string Text { get; set; }
        }

之后,我可以使用以下方法获取文本的值:

Selection[0].CountMetric.Where(x => x.Type == "SeatsToBeFilled").First().Text

这将返回“23”给我。

谢谢西尔哈米!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多