【问题标题】:c# json get only array from json resultsc# json 从 json 结果中只获取数组
【发布时间】:2021-10-21 10:43:25
【问题描述】:

我是 C# json 的新手。 我只想获取 CardTransactions 数组,“status”:0,并忽略其余的 json 值。 请问我知道如何用c#实现。

{
    "response":{
        "timeStamp":7812371,
        "totalCount":1,
        "CardTransactions":[
            {
                "transactionDate":"2021-08-16",
                "invoiceNo":"KM011782313",
                "amount":2000.00
            }
        ],
        "status":0,
        "message":"dakjalsda"
    },
    "status":null,
    "message":null
}

【问题讨论】:

  • 您是使用Newtonsoft.Json(也称为Json.NET)还是System.Text.Json进行反序列化?

标签: c# json


【解决方案1】:

创建一个看起来像这样的模型类

public class Response 
{
    [JsonProperty("CardTransactions")]
    public List<CardTransaction> CardTransactions { get; set; }

    [JsonProperty("status")]
    public int Status { get; set; }
}

public class Root
{
    [JsonProperty("response")]
    public Response Response { get; set; }
}

并使用 Newtonsoft 将 JSON 转换为对象,如下所示

Root response = JsonConvert.DeserializeObject<Root>(yourJson);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-09
    • 2011-10-04
    • 2016-08-05
    • 1970-01-01
    • 2017-01-06
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多