locked
Convert Json in C# with data RRS feed

  • Question

  • I have a lot of `json` files to convert in `C#`. In `Visual Studio 2017` I use `Paste as json` to have the schema in `C#`. Easy.
    Now I want to use my data as tests. My `json` are a bit complicated and I don't wont to spent time to create code but also I don't like the idea to have massive strings with `json`.
    Is there a tool to generate from a `json` code for the schema and its data?

    For example, if I have this `json`:

        {
           "employees": [
              { "firstName":"John" , "lastName":"Doe" },
              { "firstName":"Anna" , "lastName":"Smith" }
           ]
        }

    Visual Studio generates this code

        public class Employee
        {
            public string firstName { get; set; }
            public string lastName { get; set; }
        }
    
    
        public class Employees
        {
            public IList<Employee> employees { get; set; }
        }

    and I should have something like:

        List<Employee> employees = new List<Employee>();
        employees.Add(new Employee() { firstName = "John", lastName = "Doe" });
        employees.Add(new Employee() { firstName = "Anna", lastName = "Smith" });
    

    Thank you in advance.

    Enrico Rossini http://puresourcecode.com


    Monday, August 6, 2018 10:55 AM

All replies

  • You need to install the NewtonSoft.JSON nuget package and then you can use it to deserialize JSON to your c# class like:

    Employees employees = JsonConvert.DeserializeObject<Employees>(json);

    Refer to the following link for details:

    https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

    and here is the link to the docs:

    https://www.newtonsoft.com/json/help/html/R_Project_Documentation.htm

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Monday, August 6, 2018 12:54 PM
  • The question is not clear. I think you are asking how to write JSON data but it is not clear where the data comes from; what format the data is in and such. As best as I understand your question, it has nothing to do with JSON; you are asking how to put data into collections. It is very unclear however what you are asking.


    Sam Hobbs
    SimpleSamples.Info

    Monday, August 6, 2018 7:28 PM
  • Hi Enrico Rossini (UK),

    Thank you for posting here.

    According to your description, I have some questions wo confirm with you.

    What json format you want to get in C#? Could we load the json file directly without generate in C#? Do you want the list employees format in your description?

    Best Regards,

    Wendy   


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 7, 2018 7:30 AM
  • Thank you all for your answers. I'm trying to explain better my question.

    I'm creating a SDK for my company. This is a middleware between Xamarin (or third parties) projects and our webapi. I want to provide for each method in the SDK dummy data without calling my webapi. 

    The first idea was to save a json in a resource file but this solution isn't elegant. Another idea was to have a C# class with dummy data: in this class I should create a new instance for model and then fill it with dummy data, basically the result looks like

    List<Employee> employees = new List<Employee>();
    employees.Add(new Employee() { firstName = "John", lastName = "Doe" });
    employees.Add(new Employee() { firstName = "Anna", lastName = "Smith" });

    If you have any other idea, please tell me.


    Enrico Rossini http://puresourcecode.com



    Tuesday, August 7, 2018 9:45 PM
  • Hi Enrico Rossini (UK),

    Thank you for feedback.

    I think it is easy to save json in a file and then read it. When you generate class for json file and then write the data like employees list in your description, it is too complex. It takes more time to write data. 

    You could save your json string in a file and then read it to the format you want. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, August 10, 2018 8:39 AM
    Wednesday, August 8, 2018 3:03 AM
  • The first idea was to save a json in a resource file but this solution isn't elegant.

    Why is that? I would like to help you with that if possible.

    As for "dummy data" I am confused. I thought you wanted real, actual data, not dummy data.

    I apologize for not replying sooner but I just had open-heart surgery.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, August 23, 2018 8:38 PM