I am trying to import a JSON file into an object in C# (VS 2017 on Mac) to do further operations on it.
I have this JSON file example:
{
"subdomain": "mycompany",
"name": "MyCompany Inc",
"website": "https://www.mycompany.com",
"edition": "DIRECTORY",
"licensing":
{
"apps":
[
"boxnet"
]
},
"admin": {
"profile":
{
"firstName": "John",
"lastName": "Smith",
"email": "[email protected]",
"login": "[email protected]",
"mobilePhone": null
},
"credentials":
{
"password":
{
"value": "NotAPassword"},
"recovery_question":
{
"question": "Best Solution",
"answer": "MyOne"
}
}
}
}
}
So I generated a C# library to create an object type to import into:
using System;
namespace OrgCustom
{
using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Licensing
{
public List<string> apps { get; set; }
}
public class Profile
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string login { get; set; }
public object mobilePhone { get; set; }
}
public class Password
{
public string value { get; set; }
}
public class RecoveryQuestion
{
public string question { get; set; }
public string answer { get; set; }
}
public class Credentials
{
public Password password { get; set; }
public RecoveryQuestion recovery_question { get; set; }
}
public class Admin
{
public Profile profile { get; set; }
public Credentials credentials { get; set; }
}
public class Org
{
public string subdomain { get; set; }
public string name { get; set; }
public string website { get; set; }
public string edition { get; set; }
public Licensing licensing { get; set; }
public Admin admin { get; set; }
}
}
And so, I should be able to import the file quite easily, using the Newtownsoft.Json library?
Well, my program code is:
using System;
using System.IO;
using System.Runtime.Serialization.Json;
using RestSharp;
using Newtonsoft.Json;
using OrgCustom;
namespace TestStart
{
class Program
{
static void Main(string[] args)
{
string JSONstring = File.ReadAllText("CreateOrgExample.json");
OrgCustom.Org objOrg = JsonConvert.DeserializeObject<OrgCustom.Org>(JSONstring);
Console.WriteLine(objOrg.ToString());
}
}
}
The problem is that when I run it, I get a message on the line from the Deserialize line that JsonConvert is a type, which is not valid in the given context.
What am I doing wrong here? That line I saw in the Introduction to JSON with C# MVA course and I believe that it should work?!?!
Thanks in advance,
QuietLeni