-1

I need to retrieve specific values from API response. My response looks like one below. How can I access to [productCodeScheme] value of each pack?

dynamic api = JObject.Parse(response.Content);

// api contains
{
    "operationCode": "12200000",
    "packs": [
        {
            "pack": {
                "productCodeScheme": "ppn",
                "productCode": "15000436574634",
                "serialNumber": "0000000001",
                "batchId": "00001",
                "expiryDate": "201201"
            },
            "result": {
                "operationCode": "61020008",
                "warning": "The product code is invalid."
            }
        },
        {
            "pack": {
                "productCodeScheme": "gs1",
                "productCode": "15000436574634",
                "serialNumber": "0000000002",
                "batchId": "00001",
                "expiryDate": "201201"
            },
            "result": {
                "operationCode": "11310300",
                "information": "The pack has been marked as stolen.",
                "state": "Stolen"
            }
        }
    ]
}
Liam
  • 27,717
  • 28
  • 128
  • 190
  • go to website json2csharp - past your json there - it will give you classes; Use Newtonsoft json deserializer to deserialize json to these generated classes. Then you should find it much easier? Otherwise read docs? https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm – Leszek P Jan 29 '19 at 13:57
  • `string productCodeScheme = api.SelectToken(@"packs[0].pack.productCodeScheme ").Value()` maybe like this? – Paplusc Jan 29 '19 at 13:59
  • 1
    Hello, please read section [How do I ask good question](https://stackoverflow.com/help/how-to-ask). We expect here questions with specific problems. Your current question sounds more like a task. – Renatas M. Jan 29 '19 at 13:59

1 Answers1

0

If you want your object to stay dynamic, you could just do

dynamic result = JsonConvert.DeserializeObject<dynamic>(response.Content);

after, you can access the objects inside like below:

foreach(dynamic item in result.packs)
{
  string productCodeScheme = item.pack.productCodeScheme.ToString();
}

However, i would strongly suggest that you to deserialize your JSON responses into defined objects instead of using dynamic. dynamics are both insecure and inefficient. You can do someting like example below,

public class PackDetails
{
    public string productCodeScheme { get; set; }
    public string productCode { get; set; }
    public string serialNumber { get; set; }
    public string batchId { get; set; }
    public string expiryDate { get; set; }
}

public class Result
{
    public string operationCode { get; set; }
    public string warning { get; set; }
    public string information { get; set; }
    public string state { get; set; }
}

public class Pack
{
    public PackDetails pack { get; set; }
    public Result result { get; set; }
}

public class ResponseObject
{
    public string operationCode { get; set; }
    public List<Pack> packs { get; set; }
}

then you can deserialize a ResponseObject like below and use it

var responseObject = JsonConvert.DeserializeObject<ResponseObject>();
foreach(PackDetails item in responseObject.packs)
{
  string productCodeScheme = item.pack.productCodeScheme;
}
Cem YILMAZ
  • 134
  • 2
  • 6