-1

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

1
  • 2
    I tested on PC with VS2017 .net core 2 and it works. (after removing the last '}') Commented Nov 9, 2017 at 18:29

2 Answers 2

1

It seems to an issue with your JSON having an extra closing bracket at the end. So you might getting error at parsing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this - I am not OCD enough! I did the same!
0

The error you reported JsonConvert is a type, which is not valid in the given context has nothing to do with the fact the JSON is malformed.

I do not see a syntax error. You might check assembly references and then use a global:: qualifier to drill down to the convert call (but again it looks correct to me.)

As an aside, your Password class does not define a RecoveryQuestion property as is found in the JSON snippet. This shouldn't prevent Newtonsoft from deserializing the JSON snippet into the structure you do have defined, though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.