在JSON中访问子令牌

时间:2016-07-22 15:43:44

标签: json linq linq-to-sql json.net

在下面的JSON中,我试图访问头中的第二个数组。基本上“节点”,“百分比”,“时间”,“文件系统”需要捕获,因为我将不得不插入到SQL.My代码中提供完整的标题数组。

JObject jsonObject = JObject.Parse(jsonString);
            List<string> childTokens = new List<string>();
            foreach (var childToken in jsonObject.Children<JProperty>())
                childTokens.Add(childToken.Name);

           foreach (string childToken in childTokens)
        {
            if (jsonObject[childToken] is JObject)
            {
                JObject jObject = (JObject)jsonObject[childToken];
                var jProperty = jObject.Children<JProperty>();

            try

                {


                    if (jProperty.LastOrDefault(x => x.Name == "header") != null)
                    {
                        foreach (var headerValue in jProperty.LastOrDefault(x => x.Name == "header").Value.Children())
                            table.Columns.Add("[" + headerValue.ToString() + "]");
                        table.Columns.Add("[ID]");
                        table.Columns.Add("[comments]");
                    }

JSON示例:

 "DISK" : {
      "alarm_count" : 5,
      "column_width" : [
         12,
         14,
         16,
         14
      ],
      "header" : [
         [
            "",
            "Max Disk Usage",
            3
         ],
         [
            "Node",
            "Percentage",
            "Time",
            "File System"
         ]
      ] }

我可以在标题中有多个数组..不想硬编码..我应该总是能够在标题子标记中选择最后一个数组..请指教..谢谢

1 个答案:

答案 0 :(得分:1)

假设您的json片段有效并且是以下对象的一部分:

While processing files with ecmascript (for target web.browser):
   lib/routes.js:27:6: Unexpected token (27:6)

   While processing files with templating (for target web.browser):
   client/templates/layout/layoutDefault.html:27: Expected identifier,
   number, string, boolean, null, or a sub expression enclosed in "(", ")"
   ...           {{#if level => '500'}}
   ...
   ^

   While processing files with ecmascript (for target os.windows.x86_32):
   lib/routes.js:27:6: Unexpected token (27:6)

你可以这样做:

{
  "DISK": {
    "alarm_count": 5,
    "column_width": [
      12,
      14,
      16,
      14
    ],
    "header": [
      [
        "",
        "Max Disk Usage",
        3
      ],
      [
        "Node",
        "Percentage",
        "Time",
        "File System"
      ]
    ]
  }
}