如何找到JSON对象中最低级别的元素总数?

时间:2019-06-26 04:26:49

标签: c# json

我有一个Json,如下所示

{
  "name": "xxxx",
  "type": "ccc",
  "properties": {
    "serialNumber": {
      "value": "24-66292"
    },
    "documentLinks": {
      "productManuals": {
        "54868484": {
          "url": {
            "value": "xxxx"
          },
          "productName": {
            "value": "R02400"
          }
        }
      },
      "keystringFiles": {
        "60050588": {
          "url": {
            "value": "http://se-s-0010052.de.abb.com/stage/wc/!control.controller?action=view_document&doc_id=60050588"
          },
          "name": {
            "value": "24-66292_160.kxt"
          },
          "fileSize": {
            "value": 0.87
          },
          "addedDate": {
            "value": "2012-01-19"
          },
          "addedBy": {
            "value": "Loader"
          }
        }
      }
    }
  },
  "variables":{
   "temperature" :{
     "dataType": "number"
   },
   "modes" :{
     "normal":{
      "dataType": "string"
     },
     "fast":{
      "dataType": "string"
     }
   }
  }
}

我需要计算除根级别元素之外的其他元素总数。在此示例中,就像下面“属性”下的元素数

  1. serialNumber
  2. documentLinks-> productManuals-> 54868484-> url
  3. documentLinks-> productManuals-> 54868484-> productName
  4. documentLinks-> keystringFiles-> 60050588-> url
  5. documentLinks-> keystringFiles-> 60050588->名称
  6. documentLinks-> keystringFiles-> 60050588-> fileSize
  7. documentLinks-> keystringFiles-> 60050588-> addedDate
  8. documentLinks-> keystringFiles-> 60050588-> addedBy

在“变量”下

  1. 温度
  2. 模式->正常
  3. 模式->快速

因此元素总数为

  

8 + 3 = 11

我尝试了以下多种方法,但是我找不到能达到目的的最佳逻辑。

var ob = JObject.Parse(json);
var propCount = JObject.Parse(json).Root
                .SelectTokens("properties")
                .SelectMany(t => t.Children().OfType<JProperty>().Select(p => p.Name))
                .ToArray();
            var varCount = JObject.Parse(json).Root
                .SelectTokens("variables")
                .SelectMany(t => t.Children().OfType<JProperty>().Select(p => p.Name))
                .ToArray();
            int propCount = 0;
            int variableCount = 0;
            foreach (var prop in propCount)
            {
                propCount += ob["properties"][prop].Children().Count();
            }
            foreach (var variable in varCount)
            {
                variableCount += ob["variables"][variable].Children().Count();
            }
            var total = propCount + variableCount;

1 个答案:

答案 0 :(得分:1)

根据我的理解,我认为您有11个。您缺少属性-> serialNumber

private int GetLowestLevelCount(JToken obj)
{
  int counter = 0;
  if (obj.HasValues) // Checks if token has children. 
  {
     foreach (var children in obj.Children())
     {
         counter += GetLowestLevelCount(children);
     }

  }
  else // Lowest-level elem. 
  {
     counter +=1;
  }
  return counter;
}

使用:

var ob = JObject.Parse(json);
var mainKeys = ob.Children().Select(x => ((JProperty)x).Name);
int lowestLevel = 0;
foreach (var mainKey in mainKeys)
{
    var a = ob[mainKey];
    if (a.HasValues) //Don't bother with top-level elements 
    {
        foreach (var c in a.Children())
        {
            lowestLevel += GetLowestLevelCount(c);
        }

    }
}
Console.WriteLine(lowestLevel);