Created
August 18, 2015 10:29
-
-
Save darrenferguson/b18e62e9809846059be2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string json = | |
@"[ | |
{ | |
""X"": | |
{ | |
""Title"":""foo"", | |
""xxxx"":""xxxx"" | |
}, | |
""X"": | |
{ | |
""twat"":""step"", | |
""ass"":""b"" | |
} | |
}, | |
{ | |
""Y"": | |
{ | |
""ZZ"": | |
{ | |
""Title"":""bar"", | |
""xxxx"":""xxxx"" | |
} | |
} | |
} | |
]"; | |
JToken node = JToken.Parse(json); | |
WalkNode(node, n => | |
{ | |
foreach (var property in n.Properties().Where(x => x.Value.Type == JTokenType.String)) | |
{ | |
Console.Write(property.Name + " - > "); | |
Console.WriteLine(property.Value); | |
} | |
//if (token != null && token.Type == JTokenType.String) | |
//{ | |
// string title = token.Value<string>(); | |
// Console.WriteLine(title); | |
//} | |
}); | |
} | |
static void WalkNode(JToken node, Action<JObject> action) | |
{ | |
if (node.Type == JTokenType.Object) | |
{ | |
action((JObject)node); | |
foreach (JProperty child in node.Children<JProperty>()) | |
{ | |
WalkNode(child.Value, action); | |
} | |
} | |
else if (node.Type == JTokenType.Array) | |
{ | |
foreach (JToken child in node.Children()) | |
{ | |
WalkNode(child, action); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment