0

I want to be able to use c# to extract all the data point sin the below json. I need all the values of all the StandardId. not just a single data point but rather all instances of StandardId

{
  "CreatedBy": null,
  "CanBeRemoved": false,
  "ConfigurationItems": [ "" ],
  "ApprovalReasons": [
    {
      "AssociatedCI": "",
      "AssociatedRuleName": "BRS: ",
      "AssociatedRuleApprovalType": null,
      "AssociatedRulePartyType": "Group",
      "AssociatedRulePartyName": "Digital ",
      "AssociatedAdditionalComment": "Added to all "
    }
  ],
  "PossibleApprovers": [
    {
      "Approver": {
        "Display": "Jag",
        "StandardId": "I6",
        "RoleName": null,
        "FullName": null,
        "LineOfBusiness": null,
        "ErrorMessage": null
      },
      "IsEscalation": false,
      "IsDelegate": false
    },
    {
      "Approver": {
        "Display": "Will",
        "StandardId": "U55",
        "RoleName": null,
        "FullName": null,
        "LineOfBusiness": null,
        "ErrorMessage": null
      },
      "IsEscalation": false,
      "IsDelegate": false
    },

    {
      "Approver": {
        "Display": "Su",
        "StandardId": "U2",
        "RoleName": null,
        "FullName": null,
        "LineOfBusiness": null,
        "ErrorMessage": null
      },
      "IsEscalation": false,
      "IsDelegate": false
    }
  ],
  "OriginalApprovals": [],
  "AggregatedApproval": null,
  "IsAggregated": false,
  "AggregationId": 0,
  "UpdatedBy": null,
  "UpdatedDt": null,
  "IsGroupActive": false
}
kacalapy
  • 9,806
  • 20
  • 74
  • 119
  • 1
    Deserialize into an object, then just access the property – maccettura Jul 07 '17 at 17:28
  • yes, how? I never used json yet – kacalapy Jul 07 '17 at 17:29
  • 1
    Possible duplicate of [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – NtFreX Jul 07 '17 at 17:31
  • 2
    Write a class that looks like your json, or use [Json2CSharp](http://json2csharp.com/). Then look into using Json.NET for serialization/deserialization. Honestly this question is way too broad. – maccettura Jul 07 '17 at 17:31
  • those links all show how to put json data into a variable an reefer to any single point of data in that json. I want to get all instances of the standardid across all the iterations of approvers. – kacalapy Jul 07 '17 at 17:40
  • Once you have a list of instances that represents the data you can use Linq or whatever to get the specific details only. – Peter Bons Jul 07 '17 at 17:56

1 Answers1

1

The easiest way is the one share by @maccettura.

Create a C# object

Open json2csharp a generate your class. Visual Studio can generate it for you too:

how to generate

Deserialize your class with JSON.NET

The doc is easy to use and you can find your information in the first page.

Foo foo = JsonConvert.DeserializeObject<Foo>(json);

Demo

Try it online

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        string json = @"{
""CreatedBy"": null,
""CanBeRemoved"": false,
""ConfigurationItems"": [ """" ],
""ApprovalReasons"": [
{
  ""AssociatedCI"": """",
  ""AssociatedRuleName"": ""BRS: "",
  ""AssociatedRuleApprovalType"": null,
  ""AssociatedRulePartyType"": ""Group"",
  ""AssociatedRulePartyName"": ""Digital "",
  ""AssociatedAdditionalComment"": ""Added to all ""
}],
""PossibleApprovers"": [
{
  ""Approver"": {
    ""Display"": ""Jag"",
    ""StandardId"": ""I6"",
    ""RoleName"": null,
    ""FullName"": null,
    ""LineOfBusiness"": null,
    ""ErrorMessage"": null
  },
  ""IsEscalation"": false,
  ""IsDelegate"": false
},
{
  ""Approver"": {
    ""Display"": ""Will"",
    ""StandardId"": ""U55"",
    ""RoleName"": null,
    ""FullName"": null,
    ""LineOfBusiness"": null,
    ""ErrorMessage"": null
  },
  ""IsEscalation"": false,
  ""IsDelegate"": false
},
{
  ""Approver"": {
    ""Display"": ""Su"",
    ""StandardId"": ""U2"",
    ""RoleName"": null,
    ""FullName"": null,
    ""LineOfBusiness"": null,
    ""ErrorMessage"": null
  },
  ""IsEscalation"": false,
  ""IsDelegate"": false
}],
""OriginalApprovals"": [],
""AggregatedApproval"": null,
""IsAggregated"": false,
""AggregationId"": 0,
""UpdatedBy"": null,
""UpdatedDt"": null,
""IsGroupActive"": false
}";

        var m = JsonConvert.DeserializeObject<RootObject>(json);

        foreach(var possibleApprovers in m.PossibleApprovers)
        {
            Console.WriteLine(possibleApprovers.Approver.StandardId);
        }
    }

    public class ApprovalReason
    {
        public string AssociatedCI { get; set; }
        public string AssociatedRuleName { get; set; }
        public object AssociatedRuleApprovalType { get; set; }
        public string AssociatedRulePartyType { get; set; }
        public string AssociatedRulePartyName { get; set; }
        public string AssociatedAdditionalComment { get; set; }
    }

    public class Approver
    {
        public string Display { get; set; }
        public string StandardId { get; set; }
        public object RoleName { get; set; }
        public object FullName { get; set; }
        public object LineOfBusiness { get; set; }
        public object ErrorMessage { get; set; }
    }

    public class PossibleApprover
    {
        public Approver Approver { get; set; }
        public bool IsEscalation { get; set; }
        public bool IsDelegate { get; set; }
    }

    public class RootObject
    {
        public object CreatedBy { get; set; }
        public bool CanBeRemoved { get; set; }
        public List<string> ConfigurationItems { get; set; }
        public List<ApprovalReason> ApprovalReasons { get; set; }
        public List<PossibleApprover> PossibleApprovers { get; set; }
        public List<object> OriginalApprovals { get; set; }
        public object AggregatedApproval { get; set; }
        public bool IsAggregated { get; set; }
        public int AggregationId { get; set; }
        public object UpdatedBy { get; set; }
        public object UpdatedDt { get; set; }
        public bool IsGroupActive { get; set; }
    }
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105