2017-07-07 40 views
0

我想能夠使用c#提取所有數據點sin下面的json。我需要所有StandardId的所有值。不只是一個單一的數據點,而是所有實例StandardIdc#挑出數據表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 
} 
+1

反序列化爲一個對象,然後只需訪問屬性 – maccettura

+0

是,如何?我從來沒有使用json – kacalapy

+1

可能的重複[如何解析JSON與C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – NtFreX

回答

1

最簡單的方法是通過@maccettura的一個份額。

創建一個C#對象

打開json2csharp一個生成的類。 Visual Studio可以生成其對你太:

how to generate

反序列化類JSON.NET

doc很容易使用,你可以找到在第一頁您的信息。

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

演示

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