Write Object's contents to text file

Viewed 50

Ex.

{
    "info": {
        "language": "en-US",
        "category": "Met",
        "event": "Flood Warning",
        "responseType": "Avoid",
        "urgency": "Immediate",
        "severity": "Severe",
        "certainty": "Observed"
    }    
}

I want to write info to a text with all of its contents. Right now all I get is the name of my program plus info using string info = feed2[n].info.ToString();

1 Answers

You have to serialize your object to JSON first using something like Newtonsoft (which is available through NuGet), and then you save that string. You can't simply call ToString() on your object.

So, if you are using Newtonsoft, you should do something like the following:

string json = JsonConvert.SerializeObject(feed2[n].info);

File.WriteAllText("serialize.dat", json);
Related