C# object to JSON string

PAUL FALLOWS 21 Reputation points
2021-04-23T23:06:53.003+00:00

Hi

I'm left with an object which contains a list of items and a string - similar to this example:

 static void Main(string[] args)
        {
            Item i1 = new Item
            {
                Name = "test1",
                Value = "result1"
            };

            Item i2 = new Item
            {
                Name = "test2",
                Value = "result2"
            };

            ReturnData returnData = new ReturnData();

            List<Item> items = new List<Item>();

            items.Add(i1);
            items.Add(i2);

            returnData.Type = "type1";
            returnData.Items = items;

        }



        class ReturnData
        {

            internal string Type { get; set; }
            internal List<Item> Items { get; set; }

        }

        public class Item
        {

            public string Name { get; set; }

            public string Value { get; set; }
        }

How do I convert returnData to a json string?

I have tried:

var json = JsonConvert.SerializeObject(returnData);

Console.WriteLine(json);

But this did not work.

Many thanks
Paul

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,546 Reputation points
    2021-04-23T23:24:32.8+00:00

    In ReturnData make the members public. When I do I get:

    {"Type":"type1","Items":[{"Name":"test1","Value":"result1"},{"Name":"test2","Value":"result2"}]}
    

    So you almost had it and now you do have it.

    A .NET Fiddle for this is at ObjectToJSON.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-04-24T02:11:26.767+00:00

    When there are internal properties mark them as a JsonProperty, otherwise go with SimpleSamples recommendation.

    using System.Collections.Generic;
    using System.Diagnostics;
    using Newtonsoft.Json;
    
    namespace ObjectToJson
    {
        class Program
        {
            static void Main(string[] args)
            {
                var i1 = new Item
                {
                    Name = "test1",
                    Value = "result1"
                };
    
                var i2 = new Item
                {
                    Name = "test2",
                    Value = "result2"
                };
    
                ReturnData returnData = new ReturnData();
    
                var items = new List<Item> {i1, i2};
    
    
                returnData.Type = "type1";
                returnData.Items = items;
    
                var json = JsonConvert.SerializeObject(returnData, Formatting.Indented);
    
                Debug.WriteLine(json);
    
            }
        }
        class ReturnData
        {
            [JsonProperty]
            internal string Type { get; set; }
            [JsonProperty]
            internal List<Item> Items { get; set; }
        }
    
        public class Item
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }
    }
    

    Output

    {
      "Type": "type1",
      "Items": [
        {
          "Name": "test1",
          "Value": "result1"
        },
        {
          "Name": "test2",
          "Value": "result2"
        }
      ]
    }
    
    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.