Home > OS >  c# json get only array from json results
c# json get only array from json results

Time:10-22

I am new to c# json. I would only want to get CardTransactions array , "status": 0, and ignore the rest of the json values. May I know how to achieve with c#.

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

CodePudding user response:

Create a model class that looks that way

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; }
}

and use Newtonsoft to convert JSON to object like the following

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