Alan2
Alan2

Reputation: 24572

Serializing an object with Newtonsoft?

I am trying to serialize an object with Newtonsoft Json converter like this:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;

 string json = new Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                jlpt = "5"
            });

but it gives me an error saying that

SerializeObject does not exist.

However, when I click to check the reference I see it.

Can anyone point me to what I am doing wrong?

Upvotes: 1

Views: 2523

Answers (1)

Mostafiz
Mostafiz

Reputation: 7352

Remove new instance creation of Newtonsoft.Json.JsonConvert, because SerializeObject is a static method you don't need create a instance of the Newtonsoft.Json.JsonConvert to use it

string json = Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                jlpt = "5"
            });

also if you add using Newtonsoft.Json; to the program then you simply use like this

string json = JsonConvert.SerializeObject(new
            {
                jlpt = "5"
            });

Upvotes: 6

Related Questions