Ibanez1408
Ibanez1408

Reputation: 5058

Get a Json file from a specific folder and serialize it

I need to get json file from a specific folder in my solution. the name of the json file is "plaza.json" and the folder it is in is Data. Please see image below.

enter image description here

How do I get this file and serialize it? I have searched for some answers but the closest is this:

using (var streamReader = new StreamReader("plaza.json"))
{
    string json = streamReader.ReadToEnd();
    var deserializedObject = JsonConvert.DeserializeObject<SomeClass>(json);
}

if I use that, it doesn't see my json file

Upvotes: 0

Views: 1081

Answers (1)

Krishna
Krishna

Reputation: 1985

using (var streamReader = new StreamReader(Server.MapPath("~/Data/plaza.json"))
{
    string json = streamReader.ReadToEnd();
    var deserializedObject = JsonConvert.DeserializeObject<SomeClass>(json);
}

This should work, haven’t test but check this how file will be accessed Depending on you project type it can be server.mappath or hostingenvironment.mappath

If desktop app like win forms or wpf use this

Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Data/plaza.json");

Make sure to put copy file if modified property in build property

Upvotes: 2

Related Questions