1

So I need to parse file looking alike this:

{
pl: {
    GENERIC: {
        BACK: "COFNIJ",
        WAIT: "CZEKAJ",

        PAGES: {
            ABOUTME: {
                ID: "ID",
            },
            INFO: {
                STATUS: "STATUS",
            }
        }
    },

    TOP_MENU: {
        LOGGED: "Zalogowany",
        OPTIONS: "Opcje",
    }
},

en: {
    GENERIC: {
        BACK: "BACK",
        WAIT: "WAIT",

        PAGES: {
            ABOUTME: {
                ID: "ID",
            },
            INFO: {
                STATUS: "STATUS",
            }
        }
    },

    TOP_MENU: {
        LOGGED: "Logged",
        OPTIONS: "Options",
    }
}

}

But I don't know how many elements the file will have, so I can't create class to parse this file.

  1. My first question is how to wraped in c# the "no quotes" elements in file with double quotes to make this file json parsable ?
  2. How to parse above json file to tree data struture so it will looked like this: Sample tree, so i can output on console every leaf node with path to it, an values in "en" and "pl" subtree ?
    For example: path: generic/back en:"back" pl:"cofnij".

I've already tried to use Dictionary<string, dynamic> dictionary = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(file); to get main keys, after converting above relaxed json to valid json, but I think the tree structure will be the more effective way. Thanks for any help!

Grzeszny
  • 13
  • 1
  • 6
  • 1
    Do you have any influence in the creation process? Because `pl` and `en` are a bit..let's say shitty and not really well designed JSON. Without this and for example an attribute with this information it would be easier. – Mighty Badaboom Aug 23 '17 at 10:36
  • No :/ this JavaScript file is from angular :/ – Grzeszny Aug 23 '17 at 11:02

1 Answers1

2

Your first question was already asked here: Parsing non-standard JSON

The second question sounds a bit like this one: Your first question was already asked here: Deserialize JSON into C# dynamic object?

You could create a dynamic object

   dynamic myObj =   JsonConvert.DeserializeObject(json);
   foreach (Newtonsoft.Json.Linq.JProperty jproperty in myObj)
   {
         //..
   }

and then preprocess it to create the tree structure. This could help: How do I reflect over the members of dynamic object?

EDIT:

This is how you can convert your deserialized dynamic to a tree structure by iterating through the properties:

    public void Convert()
    {
        dynamic myObj = JsonConvert.DeserializeObject(json);
        PrintObject(myObj, 0);
    }

    private void PrintObject(JToken token, int depth)
    {
        if (token is JProperty)
        {
            var jProp = (JProperty)token;
            var spacer = string.Join("", Enumerable.Range(0, depth).Select(_ => "\t"));
            var val = jProp.Value is JValue ? ((JValue)jProp.Value).Value : "-";

            Console.WriteLine($"{spacer}{jProp.Name}  -> {val}");

            foreach (var child in jProp.Children())
            {
                PrintObject(child, depth + 1);
            }
        }
        else if (token is JObject)
        {
            foreach (var child in ((JObject)token).Children())
            {
                PrintObject(child, depth + 1);
            }
        }
    }
richej
  • 834
  • 4
  • 21