0

I'm a beginner in development so please don't judge. I need to read many (like 74) JSON files and all of them have other values. How can I read the JSON files dynamicly and can select specific values?

Currently I use Newtonsoft Json, but I don't know how I can get specific values. So as a sample this is one of the JSON file.

{
    "value": [
        {
            "resourceRef": "/accessControlLists/abc123",
            "resourceId": "abc123",
            "resourceMetadata": {
                "resourceName": "SCVMM Default ACL"
            },
            "etag": "W/\"ea02b098-bcd1-406a-abff-ab380011d510\"",
            "instanceId": "e123f536-ffc1-429e-b67e-50627b1613c8",
            "properties": {
                "provisioningState": "Succeeded",
                "aclRules": [
                    {
                        "resourceRef": "/accessControlLists/abc123/aclRules/def456",
                        "resourceId": "def456",
                        "etag": "W/\"ea02b098-bcd1-406a-abff-ab380011d510\"",
                        "instanceId": "e3467412-e9c7-48a0-9d45-bdaedbe72d2d",
                        "properties": {
                            "provisioningState": "Succeeded",
                            "protocol": "All",
                            "sourcePortRange": "*",
                            "destinationPortRange": "*",
                            "action": "Allow",
                            "sourceAddressPrefix": "*",
                            "destinationAddressPrefix": "*",
                            "priority": "65000",
                            "description": "SCVMM Default Rule - Allow All",
                            "type": "Inbound",
                            "logging": "Enabled"
                        }
                    }
                ],
                "ipConfigurations": [],
                "subnets": []
            }
        }
    ],
    "nextLink": ""
}

The second file is like this.

    {
      "resourceRef": "/virtualNetworkManager/",
      "instanceId": "00000000-0000-0000-0000-000000000000",
      "properties": {
        "provisioningState": "Succeeded",
        "distributedRouterState": "Enabled",
        "networkVirtualizationProtocol": "VXLAN"
      }
    }

So how can I read as a sample the description of the first file? And how can I do it dynamicly?

Thanks for help!

Edit: dynamic with JsonConvert worked for me.

        string path = string.Empty;
        Console.Write("Please enter json file path: ");
        path = Console.ReadLine();

        dynamic obj = JObject.Parse(File.ReadAllText(path));
        for (int i = 0; i < obj.value.Count; i++)
        {
            for (int j = 0; j < obj.value[i].properties.aclRules.Count; j++)
            {
                Console.WriteLine(obj.value[i].properties.aclRules[j].properties.description);
            }
        }
Varusal
  • 15
  • 1
  • 7
  • It doesn't matter if you are beginner. Rules applied to all. keyword: newtonsoft – T.S. Nov 18 '16 at 23:27
  • You could use [`SelectToken`](http://www.newtonsoft.com/json/help/html/QueryJsonSelectToken.htm). See [here](https://stackoverflow.com/questions/1698175/what-is-the-json-net-equivalent-of-xmls-xpath-selectnodes-selectsinglenode/1711407#1711407) or [here](https://stackoverflow.com/questions/19645501/searching-for-a-specific-jtoken-by-name-in-a-jobject-hierarchy/19646950#19646950). Start with [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c). – dbc Nov 18 '16 at 23:31
  • Thanks. But this all doesn't really help. When I use classes I have more than 30 classes. Is there no other way to do it? – Varusal Nov 18 '16 at 23:36
  • 1
    Yes, using `dynamic` and `JObject`. See [here](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net). –  Nov 18 '16 at 23:43
  • 1
    Though, there are ways to generate most/all of your classes from the JSON. Google "generate c# classes from json". It's not perfect, but it'll get you 90% of the way there. I recommend you take this approach, not the `dynamic` approach. –  Nov 18 '16 at 23:48
  • @Amy, Visual Studio already have this feature - generating classes based on JSON. `Edit` -> `Paste Special` -> `Paste JSON As Classes` – Fabio Nov 19 '16 at 00:19

1 Answers1

1

How can I read the JSON files dynamically and can select specific values? So how can I read as a sample the description of the first file? And how can I do it dynamically

With the dynamic functionality :

dynamic obj = JsonConvert.DeserializeObject(json);

var description = obj.value[0].properties.aclRules[0].properties.description;
Jim
  • 2,974
  • 2
  • 19
  • 29