StackOverflow Q32782937

CSharp · Console

/Program.cs

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
					
public class Program
{
	public static void Main()
	{
		string json = @"
            {
                ""appointmentid"": 4,
                ""policyid"": 1,
                ""guid"": ""00000000-0000-0000-0000-000000000000"",
                ""number"": ""1234567890"",
                ""ampm"": ""false"",
                ""date"": ""2015-09-08T00:00:00"",
                ""vehicle"": {
                    ""id"": 1,
                    ""guid"": ""00000000-0000-0000-0000-000000000000"",
                    ""make"": null,
                    ""model"": null
                },
                ""installer"": {
                    ""installerid"": ""1"",
                    ""name"": ""Installer 1"",
                    ""contact"": ""qwerty"",
                    ""qascore"": ""0"",
                    ""address1"": ""qwerty"",
                    ""address2"": ""qwerty"",
                    ""address3"": null,
                    ""address4"": null,
                    ""city"": ""qwertyu"",
                    ""county"": ""qwertyu"",
                    ""postcode"": ""asdfghj"",
                    ""country"": ""GB"",
                    ""email"": ""asdfghj"",
                    ""web"": ""asdfghjk"",
                    ""archived"": false
                },
                ""installations"": [
                    {
                        ""installationid"": 6,
                        ""installationstatus"": {
                            ""installationstatusid"": 4,
                            ""installationstatus"": ""FAIL""
                        },
                        ""isactive"": true
                    },
                    {
                        ""installationid"": 7,
                        ""installationstatus"": {
                            ""installationstatusid"": 1,
                            ""installationstatus"": ""NEW""
                        },
                        ""isactive"": false
                    }
                ],
                ""archived"": false
            }";
		
		var dict = JsonHelper.DeserializeAndFlatten(json);
		foreach (var kvp in dict)
		{
			int i = kvp.Key.LastIndexOf(".");
			string key = (i > -1 ? kvp.Key.Substring(i + 1) : kvp.Key);
			Match m = Regex.Match(kvp.Key, @"\.([0-9]+)\.");
			if (m.Success) key += m.Groups[1].Value;
			Console.WriteLine(key + ": " + kvp.Value);
		}
	}
}

public static class JsonHelper
{
	public static Dictionary<string, object> DeserializeAndFlatten(string json)
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        JToken token = JToken.Parse(json);
        FillDictionaryFromJToken(dict, token, "");
        return dict;
    }

    private static void FillDictionaryFromJToken(Dictionary<string, object> dict, JToken token, string prefix)
    {
        switch (token.Type)
        {
            case JTokenType.Object:
                foreach (JProperty prop in token.Children<JProperty>())
                {
                    FillDictionaryFromJToken(dict, prop.Value, Join(prefix, prop.Name));
                }
                break;

            case JTokenType.Array:
                int index = 0;
                foreach (JToken value in token.Children())
                {
                    FillDictionaryFromJToken(dict, value, Join(prefix, index.ToString()));
                    index++;
                }
                break;

            default:
                dict.Add(prefix, ((JValue)token).Value);
                break;
        }
    }

    private static string Join(string prefix, string name)
    {
        return (string.IsNullOrEmpty(prefix) ? name : prefix + "." + name);
    }

}
;