如何使用Newtonsoft从JSON对象获取JSON密钥

时间:2018-07-25 17:25:31

标签: c# json.net

我需要迭代嵌套的json,并且需要从最终的json对象中提取一些固定的属性值。在下面的JSON中,我需要为title / ref和其他属性做一些内部逻辑。如何解析以下json并获取type,employeeFirstName,employeeLastName and address

之类的密钥

JSON:

"properties": {
    "type": {
      "description": "Defines type of object being represented",
      "type": "string",
      "enum": [
        "employee"
      ]
    },
    "employeeFirstName": {
      "title": "First Name",
      "description": "Employee first name",
      "type": "string"
    },
    "employeeLastName": {
      "title": "Last Name",
      "description": "Employee Last name",
      "type": "string"
    },
    "address": {
      "title": "Address",
      "description": "Employee present address ",
      "$ref": "#/definitions/address"
        }
}

代码:

        var jsonObject = JObject.Parse(jsonFileContent);

        var propertyContent = JObject.Parse(jsonObject["properties"].ToString());
        var definitionContent = JObject.Parse(jsonObject["definitions"].ToString());

        for (int i = 0; i < propertyContent.Count; i++)
        {
            // Here I need to pass the value dynamically.
            //For Example, first iteration, type needs to be passed,
            //in second iteration, employeeFirstName needs to be passed and so on
            var tempContent = propertyContent["address"].ToString();

            var def = JObject.Parse(tempContent);

            for (int j = 0; j < def.Count; j++)
            {
                //Some working logic is added based on the specific key

            }
        }

在上面的代码中,我需要为每次迭代在步骤var tempContent = propertyContent["address"].ToString();中传递动态密钥。因此,有没有办法从propertyContent捕获所有密钥?

1 个答案:

答案 0 :(得分:1)

这是一个快速而肮脏的例子:

using Newtonsoft.Json.Linq;
using System;

public class Program
{
    public static void Main()
    {
        {
            string sb = "           {\"properties\": {" +
                "    \"type\": {" +
                "      \"description\": \"Defines type of object being represented\"," +
                "      \"type\": \"string\"," +
                "      \"enum\": [" +
                "        \"employee\"" +
                "      ]" +
                "    }," +
                "    \"employeeFirstName\": {" +
                "      \"title\": \"First Name\"," +
                "      \"description\": \"Employee first name\"," +
                "      \"type\": \"string\"" +
                "    }," +
                "    \"employeeLastName\": {" +
                "      \"title\": \"Last Name\"," +
                "      \"description\": \"Employee Last name\"," +
                "      \"type\": \"string\"" +
                "    }," +
                "    \"address\": {" +
                "      \"title\": \"Address\"," +
                "      \"description\": \"Employee present address \"," +
                "      \"$ref\": \"#/definitions/address\"" +
                "        }" +
                "}}";
            var jsonObject = JObject.Parse(sb);

            var props = jsonObject.GetValue("properties");

            foreach (var prop in props.Values<JProperty>())
            {
                switch (prop.Name)
                {
                    case "type":
                    Console.WriteLine("Handling type: " + prop.Value);
                    break;
                    case "employeeFirstName":
                    Console.WriteLine("employeeFirstName: " + prop.Value);
                    break;
                }
            }
        }
    }
}

请参见dotnetfiddle here