2017-06-21 65 views
-3

您好我需要一種方法來遍歷給定的json數組的json對象,並根據每個json對象的「F」屬性中的反斜槓數量(/)動態地爲每個json對象添加名爲「depth」的屬性我需要爲我的項目。如何遍歷json字符串(json對象的json數組)並使用c#動態添加屬性?

這裏是JSON字符串:

[ 
    { 
     "A":"0", 
     "B":"0", 
     "ID":"000001", 
     "C":"Class", 
     "D":"/000001", 

     "F":"/Class", 
     "children":[ 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000004", 
      "C":"Test1", 
      "D":"/000001/000004", 

      "F":"/Class/Test1" 
     }, 
     { 
      "A":"0", 
      "B":"0", 
      "ID":"000005", 
      "C":"aaa1_test_2", 
      "D":"/000001/000005", 

      "F":"/Class/aaa1_test_2", 
      "children":[ 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"000006", 
        "C":"abcd", 
        "D":"/000001/000005/000006", 

        "F":"/Class/aaa1_test_2/abcd" 
       } 
      ] 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000007", 
      "C":"16.2.2017", 
      "D":"/000001/000007", 

      "F":"/Class/16.2.2017" 
     }, 
     { 
      "A":"0", 
      "B":"1", 
      "ID":"000008", 
      "C":"008000_02-Core Dia:-CORE DIAMETER", 
      "D":"/000001/000008", 


      "F":"/Class/008000_02-Core Dia:-CORE DIAMETER", 
      "children":[ 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"000027", 
        "C":"1wa", 
        "D":"/000001/000008/000027", 

        "F":"/Class/008000_02-Core Dia:-CORE DIAMETER/@1wa" 
       } 
      ] 
     }, 
     { 
      "A":"0", 
      "B":"1", 
      "ID":"000009", 
      "C":"20.2.2017", 
      "D":"/000001/000009", 


      "F":"/Class/20.2.2017", 
      "children":[ 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"000010", 
        "C":"TEST-005", 
        "D":"/000001/000009/000010", 

        "F":"/Class/20.2.2017/TEST-005" 
       } 
      ] 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000011", 
      "C":"3.3.2017", 
      "D":"/000001/000011", 

      "F":"/Class/3.3.2017" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000016", 
      "C":"TEST100", 
      "D":"/000001/000016", 

      "F":"/Class/TEST100" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000017", 
      "C":"Test101", 
      "D":"/000001/000017", 

      "F":"/Class/Test101" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000019", 
      "C":"Test102", 
      "D":"/000001/000019", 

      "F":"/Class/Test102" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000024", 
      "C":"Godrej", 
      "D":"/000001/000024", 

      "F":"/Class/God" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000025", 
      "C":"T", 
      "D":"/000001/000025", 

      "F":"/Class/T" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000026", 
      "C":"K_Test", 
      "D":"/000001/000026", 

      "F":"/Class/K_Test" 
     }, 
     { 
      "A":"1", 
      "B":"0", 
      "ID":"000028", 
      "C":"RB_TEST1", 
      "D":"/000001/000028", 

      "F":"/Class/RB_TEST1" 
     }, 
     { 
      "A":"0", 
      "B":"0", 
      "ID":"900004", 
      "C":"Comps", 
      "D":"/000001/900004", 


      "F":"/Class/Comps", 
      "children":[ 
       { 
        "A":"0", 
        "B":"1", 
        "ID":"900006", 
        "C":"Capacitors", 
        "D":"/000001/900004/900006", 


        "F":"/Class/Comps/Capacitors", 
        "children":[ 
        { 
         "A":"1", 
         "B":"1", 
         "ID":"000015", 
         "C":"Test2", 
         "D":"/000001/900004/900006/000015", 

         "F":"/Class/Comps/Capacitors/Test2" 
        }, 
        { 
         "A":"0", 
         "B":"1", 
         "ID":"000018", 
         "C":"Test3", 
         "D":"/000001/900004/900006/000018", 


         "F":"/Class/Comps/Capacitors/Test3", 
         "children":[ 
          { 
           "A":"1", 
           "B":"0", 
           "ID":"000020", 
           "C":"Test4", 
           "D":"/000001/900004/900006/000018/000020", 
           "F":"/Class/Comps/Capacitors/Test3/Test4" 
          } 
         ] 
        } 
        ] 
       }, 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"900007", 
        "C":"Induct", 
        "D":"/000001/900004/900007", 

        "F":"/Class/Comps/Induct" 
       }, 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"900008", 
        "C":"ICS", 
        "D":"/000001/900004/900008", 

        "F":"/Class/Comps/ICS" 
       } 
      ] 
     } 
     ] 
    } 
] 

我需要的輸出是這樣的:

[ 
    { 
     "A":"0", 
     "B":"0", 
     "ID":"000001", 
     "C":"Class", 
     "D":"/000001", 
     "depth":1, 
     "F":"/Class", 
     "children":[ 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000004", 
      "C":"Test1", 
      "D":"/000001/000004", 
      "depth":2, 
      "F":"/Class/Test1" 
     }, 
     { 
      "A":"0", 
      "B":"0", 
      "ID":"000005", 
      "C":"aaa1_test_2", 
      "D":"/000001/000005", 
      "depth":2, 
      "F":"/Class/aaa1_test_2", 
      "children":[ 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"000006", 
        "C":"abcd", 
        "D":"/000001/000005/000006", 
        "depth":3, 
        "F":"/Class/aaa1_test_2/abcd" 
       } 
      ] 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000007", 
      "C":"16.2.2017", 
      "D":"/000001/000007", 
      "depth":2, 
      "F":"/Class/16.2.2017" 
     }, 
     { 
      "A":"0", 
      "B":"1", 
      "ID":"000008", 
      "C":"008000_02-Core Dia:-CORE DIAMETER", 
      "D":"/000001/000008", 
      "depth":2, 
      "F":"/Class/008000_02-Core Dia:-CORE DIAMETER", 
      "children":[ 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"000027", 
        "C":"1wa", 
        "D":"/000001/000008/000027", 
        "depth":3, 
        "F":"/Class/008000_02-Core Dia:-CORE DIAMETER/@1wa" 
       } 
      ] 
     }, 
     { 
      "A":"0", 
      "B":"1", 
      "ID":"000009", 
      "C":"20.2.2017", 
      "D":"/000001/000009", 
      "depth":2, 
      "F":"/Class/20.2.2017", 
      "children":[ 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"000010", 
        "C":"TEST-005", 
        "D":"/000001/000009/000010", 
        "depth":3, 
        "F":"/Class/20.2.2017/TEST-005" 
       } 
      ] 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000011", 
      "C":"3.3.2017", 
      "D":"/000001/000011", 
      "depth":2, 
      "F":"/Class/3.3.2017" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000016", 
      "C":"TEST100", 
      "D":"/000001/000016", 
      "depth":2, 
      "F":"/Class/TEST100" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000017", 
      "C":"Test101", 
      "D":"/000001/000017", 
      "depth":2, 
      "F":"/Class/Test101" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000019", 
      "C":"Test102", 
      "D":"/000001/000019", 
      "depth":2, 
      "F":"/Class/Test102" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000024", 
      "C":"Godrej", 
      "D":"/000001/000024", 
      "depth":2, 
      "F":"/Class/God" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000025", 
      "C":"T", 
      "D":"/000001/000025", 
      "depth":2, 
      "F":"/Class/T" 
     }, 
     { 
      "A":"1", 
      "B":"1", 
      "ID":"000026", 
      "C":"K_Test", 
      "D":"/000001/000026", 
      "depth":2, 
      "F":"/Class/K_Test" 
     }, 
     { 
      "A":"1", 
      "B":"0", 
      "ID":"000028", 
      "C":"RB_TEST1", 
      "D":"/000001/000028", 
      "depth":2, 
      "F":"/Class/RB_TEST1" 
     }, 
     { 
      "A":"0", 
      "B":"0", 
      "ID":"900004", 
      "C":"Comps", 
      "D":"/000001/900004", 
      "depth":2, 
      "F":"/Class/Comps", 
      "children":[ 
       { 
        "A":"0", 
        "B":"1", 
        "ID":"900006", 
        "C":"Capacitors", 
        "D":"/000001/900004/900006", 
        "depth":3, 
        "F":"/Class/Comps/Capacitors", 
        "children":[ 
        { 
         "A":"1", 
         "B":"1", 
         "ID":"000015", 
         "C":"Test2", 
         "D":"/000001/900004/900006/000015", 
         "depth":4, 
         "F":"/Class/Comps/Capacitors/Test2" 
        }, 
        { 
         "A":"0", 
         "B":"1", 
         "ID":"000018", 
         "C":"Test3", 
         "D":"/000001/900004/900006/000018", 
         "depth":4, 
         "F":"/Class/Comps/Capacitors/Test3", 
         "children":[ 
          { 
           "A":"1", 
           "B":"0", 
           "ID":"000020", 
           "C":"Test4", 
           "D":"/000001/900004/900006/000018/000020", 
           "depth":5, 
           "F":"/Class/Comps/Capacitors/Test3/Test4" 
          } 
         ] 
        } 
        ] 
       }, 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"900007", 
        "C":"Induct", 
        "D":"/000001/900004/900007", 
        "depth":2, 
        "F":"/Class/Comps/Induct" 
       }, 
       { 
        "A":"1", 
        "B":"1", 
        "ID":"900008", 
        "C":"ICS", 
        "D":"/000001/900004/900008", 
        "depth":2, 
        "F":"/Class/Comps/ICS" 
       } 
      ] 
     } 
     ] 
    } 
] 

例如,如果你看一下第一個JSON對象 「F」 屬性包含1回斜線因此「深度「屬性值應該是1.

+0

_「這是代碼」_ - 您沒有發佈任何代碼。只是JSON數據。顯而易見的方法是將源數據反序列化爲實際的對象模型,然後使用新的'depth'屬性將數據作爲JSON進行研究。根據您使用的處理JSON的方式,您可以反序列化爲具有'depth'屬性的對象(該對象將保持未初始化狀態),設置屬性然後重新序列化,或者僅從一個數據中複製沒有財產的結構給另一個擁有財產的財產,在這個財產中你將它設定爲你想要的價值。 –

回答

0

我假設您只需要在響應中添加另一個property(F),我寧願在C#代碼中添加新屬性比改變Json。一旦你映射Json響應與響應類對象(C#),使用Foreach循環通過每個children對象檢查你想要的條件和add F

試試這個。

的foreach(在root.children變種X){//條件}

1

您可以使用Newtonsoft.Json包和JPath(http://goessner.net/articles/JsonPath/

,這樣你的代碼可以是這樣的:

var parsedJson = JToken.Parse(json); 
var nodes = parsedJson.SelectTokens("$..*"); 
foreach (var node in nodes.OfType<JObject>()) 
{ 
    var pathValue = node.Property("F")?.Value?.Value<string>(); 
    if (string.IsNullOrWhiteSpace(pathValue)) 
    { 
     continue; 
    } 

    var depth = pathValue.Split(new[] { '/' }, StringSplitOptions.None).Length; 
    node.Add("depth", depth - 1); 
} 
var modifiedJson = parsedJson.ToString();