0

I'm trying to create this JSON string in C#

[
  {
    accountId = 123,
    reportType = 1,
    reportTypeDesc = "Daily"
  },
  {
    accountId = 123,
    reportType = 1,
    reportTypeDesc = "Daily"
  },
  {
    accountId = 123,
    reportType = 1,
    reportTypeDesc = "Daily"
  }
]

I tried this way to do this json , here is my code :

    var body = new []

            new
            {
                accountId = 123,
                reportType = 1,
                reportTypeDesc = "Daily"
            },
               new
            {
                accountId = 123,
                reportType = 1,
                reportTypeDesc = "Daily"
            },
               new
            {
               accountId = 123,
               reportType = 1,
               reportTypeDesc = "Daily"
            },

But I have compiler errors in "new[]" area

What is the right way to make this Json ? i tried all lots of different variation but nothing works

5
  • 3
    You forgot the braces: new[] { ... } Commented Dec 31, 2015 at 15:19
  • 1
    I have been happy using this framework for my json serialization needs: newtonsoft.com/json If you're using visual studio, you can add it to your solution using nuget. Commented Dec 31, 2015 at 15:23
  • stackoverflow.com/questions/331976/… might help as well. Commented Dec 31, 2015 at 15:27
  • Step 1. Install-Package Newtonsoft.Json Step 2. JsonConvert.SerializeObject<Sample>(sample); Commented Dec 31, 2015 at 15:49
  • don't forget to mark answer as correct Commented Jan 2, 2016 at 18:35

2 Answers 2

1

Try to use newtonsoft Json.NET for this, see how:

var body = new object []{
  new
  {
    accountId = 123,
    reportType = 1,
    reportTypeDesc = "Daily"
  },
 new
  {
      accountId = 123,
      reportType = 1,
      reportTypeDesc = "Daily"
  },
 new
  {
     accountId = 123,
     reportType = 1,
     reportTypeDesc = "Daily"
    },
};
var jsonBody = JsonConvert.SerializeObject(body);

See it working in my .NET Fiddle.

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

Comments

0

The best way to work around with Json in c# or VB.Net is to have a great library like, Newtonsoft.Json. This will make your whole work lots easier and quicker. So, just download that library and try coding.

Remedy

public class MainClass
{
    public class Items
    {
        public int accountId, reportType;
        public string reportTypeDesc;
        public Items()
        {

        }
        public Items(int accountId, int reportType, string reportTypeDesc)
        {
            this.accountId = accountId;
            this.reportType = reportType;
            this.reportTypeDesc = reportTypeDesc;
        }
    }
    public List<Items> allItems = new List<Items>();
    public string toJson() =>
        JsonConvert.SerializeObject(allItems);
}

Execution

private void Form1_Load(object sender, EventArgs e)
    {
        MainClass m = new MainClass();
        m.allItems.Add(new MainClass.Items(123, 1, "Daily"));
        m.allItems.Add(new MainClass.Items(123, 1, "Daily"));
        m.allItems.Add(new MainClass.Items(123, 1, "Daily"));
        string json = m.toJson();
        richTextBox1.AppendText(json);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.