Quantcast
Channel: codetitans Discussions Rss Feed
Viewing all articles
Browse latest Browse all 23

New Post: JSonObject IEnumerable?

$
0
0
IJSonObject doesn't allow enumeration by design. What would you like to enumerate - array items or object items - depending on the data inside? That would lead to lots of unexpected exceptions.

The enumeration approach, you like to have, could be done using ArrayItems and ObjectItems properties.
Take a look on following sample, that should answer all questions:
var str = @"[{""title"":""Title1"",""children"":[{""title"":""Child1"",""children"":[{""title"":""grandchild1"",""children"":[{""title"":""Huh""}]}] }] }, {""title"": ""Title2"" }]";

JSonReader jr = new JSonReader();
IJSonObject json = jr.ReadAsJSonObject(str);
IEnumerable items = json.ArrayItems;

foreach (var arrayItem in json.ArrayItems)
{
    //Console.WriteLine(arrayItem.ToString());
    foreach (var objItem in arrayItem.ObjectItems)
    {
        Console.WriteLine(objItem);
    }
}
This will print out all the top-level property names, i.e.: title, children, title.


Cheers,
Pawel

Viewing all articles
Browse latest Browse all 23

Latest Images

Trending Articles



Latest Images