jeet jeet - 6 months ago 9
JSON Question

Quick and Easy Deserialization and To Text JSON

So I have a big JSON file with lists and stuff that looks like this: link

I have also devised the necessary classes using json2csharp.com.

My problem is, I need to output the whole data into a

textBox
. I just need the text representation of this data.

I can obviously call each member, loop through al the list and manage their textual representation, but is there a quick and dirty way to just output the string? I am using JSON.net.

Thanks.

EDIT: I know how to do it using .net objeccts as given here:

www.newtonsoft.com/json/help/html/serializingjson.htm

I was looking for a one go method.

Answer

You can serialize a graph of objects as long as you have the right data structures. For example, you can see how the Bar objects are created as an array of objects:

public class Foo {

    private List<Bar> names;
    private string name;

    public Foo() {
        this.name = "Name";
        this.names = new List<Bar>() {
            new Bar("a"),
            new Bar("b"),
            new Bar("c")
        };
    }

    public string Name {
        get {
            return this.name;
        }
    }

    public List<Bar> Names {
        get {
            return this.names;
        }
    }
}

public class Bar {
    public Bar(string name) {
        this.Name = name;
    }

    public string Name {
        get;
        private set;
    }
}

var o = new Foo();
Debug.WriteLine(JsonConvert.SerializeObject(o));