Read and write JSON in c# WinForms
-
I wanted to mimic .net core and use an appSettings.Json file, well it didn't work out. Guess .Net Core Extensions won't work in my project. So I wrote some code using Newtonsoft, could not read the file and parse out pure JSON. Well I was able to read the file but not able to deserialize it. Searched the internet and found a couple of solutions that didn't pan out as well. So I searching for a NuGet package and found one, but no documentation. Anybody used or found a package that they like? I just want to read and write my MongoDB connection string. Side Note, really liking Mongo for this project! except for this part. But I got some automated code to load and program Mongo going now, just need to finish this up.
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
I wanted to mimic .net core and use an appSettings.Json file, well it didn't work out. Guess .Net Core Extensions won't work in my project. So I wrote some code using Newtonsoft, could not read the file and parse out pure JSON. Well I was able to read the file but not able to deserialize it. Searched the internet and found a couple of solutions that didn't pan out as well. So I searching for a NuGet package and found one, but no documentation. Anybody used or found a package that they like? I just want to read and write my MongoDB connection string. Side Note, really liking Mongo for this project! except for this part. But I got some automated code to load and program Mongo going now, just need to finish this up.
If it ain't broke don't fix it Discover my world at jkirkerx.com
Newtonsoft works for me:
string strConnect = "my connection string"; string json = Newtonsoft.Json.JsonConvert.SerializeObject(strConnect);
...
string readback = Newtonsoft.Json.JsonConvert.DeserializeObject(json);It doesn't have to be a string, it can be any class instance! So a "Settings" class that includes your connection information maybe? Or a list of Settings objects?
List readback = Newtonsoft.Json.JsonConvert.DeserializeObject\>(json);
The world is the mollusc of your choice!
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
I wanted to mimic .net core and use an appSettings.Json file, well it didn't work out. Guess .Net Core Extensions won't work in my project. So I wrote some code using Newtonsoft, could not read the file and parse out pure JSON. Well I was able to read the file but not able to deserialize it. Searched the internet and found a couple of solutions that didn't pan out as well. So I searching for a NuGet package and found one, but no documentation. Anybody used or found a package that they like? I just want to read and write my MongoDB connection string. Side Note, really liking Mongo for this project! except for this part. But I got some automated code to load and program Mongo going now, just need to finish this up.
If it ain't broke don't fix it Discover my world at jkirkerx.com
I'm not sure this will help, but why not use the built-in JSON package in .NET-framework 4.0 or later? For instance try this: 1) Create a new console application. 2) Add a reference to System.Web.Extensions.dll. 3) Paste the following code:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var list = new List();
list.Add(new Person() { Name = "Dude", Age = 21 });
list.Add(new Person() { Name = "Chap", Age = 28 });var json = new JavaScriptSerializer(); var result = json.Serialize(list); var listFromJson = json.Deserialize(result, typeof(List)) as List; } } class Person { public string Name { get; set; } public int Age { get; set; } }
}
Just debug and step through the code to verify it works :) I have serialized and deserialized a List with a custom class. You can of course serialize/deserialize whatever you want. Best regards, /Steffe
-
I wanted to mimic .net core and use an appSettings.Json file, well it didn't work out. Guess .Net Core Extensions won't work in my project. So I wrote some code using Newtonsoft, could not read the file and parse out pure JSON. Well I was able to read the file but not able to deserialize it. Searched the internet and found a couple of solutions that didn't pan out as well. So I searching for a NuGet package and found one, but no documentation. Anybody used or found a package that they like? I just want to read and write my MongoDB connection string. Side Note, really liking Mongo for this project! except for this part. But I got some automated code to load and program Mongo going now, just need to finish this up.
If it ain't broke don't fix it Discover my world at jkirkerx.com
Using .NET Core Configuration with legacy projects - Ben Foster[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I'm not sure this will help, but why not use the built-in JSON package in .NET-framework 4.0 or later? For instance try this: 1) Create a new console application. 2) Add a reference to System.Web.Extensions.dll. 3) Paste the following code:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var list = new List();
list.Add(new Person() { Name = "Dude", Age = 21 });
list.Add(new Person() { Name = "Chap", Age = 28 });var json = new JavaScriptSerializer(); var result = json.Serialize(list); var listFromJson = json.Deserialize(result, typeof(List)) as List; } } class Person { public string Name { get; set; } public int Age { get; set; } }
}
Just debug and step through the code to verify it works :) I have serialized and deserialized a List with a custom class. You can of course serialize/deserialize whatever you want. Best regards, /Steffe
I saw a couple examples of that while searching, but passed on it because I had already written code.
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
Newtonsoft works for me:
string strConnect = "my connection string"; string json = Newtonsoft.Json.JsonConvert.SerializeObject(strConnect);
...
string readback = Newtonsoft.Json.JsonConvert.DeserializeObject(json);It doesn't have to be a string, it can be any class instance! So a "Settings" class that includes your connection information maybe? Or a list of Settings objects?
List readback = Newtonsoft.Json.JsonConvert.DeserializeObject\>(json);
The world is the mollusc of your choice!
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
I've been working with Newtonsoft this morning and still can't deserialize. hmm... I'll post my code
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
I wanted to mimic .net core and use an appSettings.Json file, well it didn't work out. Guess .Net Core Extensions won't work in my project. So I wrote some code using Newtonsoft, could not read the file and parse out pure JSON. Well I was able to read the file but not able to deserialize it. Searched the internet and found a couple of solutions that didn't pan out as well. So I searching for a NuGet package and found one, but no documentation. Anybody used or found a package that they like? I just want to read and write my MongoDB connection string. Side Note, really liking Mongo for this project! except for this part. But I got some automated code to load and program Mongo going now, just need to finish this up.
If it ain't broke don't fix it Discover my world at jkirkerx.com
I'm completely missing something here. This should work! But var settings is always null. Settings Wireless.Models.Settings DbConnection = null SmtpConnection = null My json file: IN the project folder, marked as Content, Copy if newer
{
"Settings": {
"DbConnection": {
"Connection": "mongodb://admin:Password@localhost",
"Database": "database"
},
"SmtpConnection": {
"Server": "smtp.gmail.com",
"User": "emailAddress",
"Pass": "password",
"Port": "587",
"SSL": "True"
}
}
}My Model, I double checked it to make sure it matches
public class Settings
{
public MongoDbConfig DbConnection { get; set; }
public SmtpConfig SmtpConnection { get; set; }
}public class MongoDbConfig { public string Connection { get; set; } public string Database { get; set; } } public class SmtpConfig { public string Server { get; set; } public string User { get; set; } public string Pass { get; set; } public int Port { get; set; } public bool SSL { get; set; } }
My function to read the file, I verified the file in jsonText
public static void GetSettings()
{
var appPath = Path.Combine(Application.StartupPath, "appSettings.json");
var jsonText = File.ReadAllText(appPath);
var settings = JsonConvert.DeserializeObject(jsonText);
Console.Write(appPath);
}jsonText output
jsonText = "{\r\n \"Settings\": {\r\n \"DbConnection\": {\r\n \"Connection\": \"mongodb://admin:Password@localhost\",\r\n \"Database\": \"wirelessDb\"\r\n },\r\n \"SmtpConnection\": {\r\n \"Server\": \"smtp.gmail.com\",\r\n
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
Using .NET Core Configuration with legacy projects - Ben Foster[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I took a look at the link and gave it a try, and was able to load the example, but not able to figure out how to use the object.
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
I wanted to mimic .net core and use an appSettings.Json file, well it didn't work out. Guess .Net Core Extensions won't work in my project. So I wrote some code using Newtonsoft, could not read the file and parse out pure JSON. Well I was able to read the file but not able to deserialize it. Searched the internet and found a couple of solutions that didn't pan out as well. So I searching for a NuGet package and found one, but no documentation. Anybody used or found a package that they like? I just want to read and write my MongoDB connection string. Side Note, really liking Mongo for this project! except for this part. But I got some automated code to load and program Mongo going now, just need to finish this up.
If it ain't broke don't fix it Discover my world at jkirkerx.com
Searched for complex deserialization, and ran across this website [json2csharp - generate c# classes from json](http://json2csharp.com/) and code to deserialize New Model
public class DbConnection
{
public string Connection { get; set; }
public string Database { get; set; }
}public class SmtpConnection
{
public string Server { get; set; }
public string User { get; set; }
public string Pass { get; set; }
public string Port { get; set; }
public string SSL { get; set; }
}public class Settings
{
public DbConnection DbConnection { get; set; }
public SmtpConnection SmtpConnection { get; set; }
}public class RootObject
{
public Settings Settings { get; set; }
}Code to deserialize
JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize(jsonText);If it ain't broke don't fix it Discover my world at jkirkerx.com