1

我需要迭代嵌套的 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

4

1 回答 1

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 。

于 2018-07-25T17:46:20.313 回答