2011-06-01 12:59:21 8 Comments
I have classes like these:
class MyDate
{
int year, month, day;
}
class Lad
{
string firstName;
string lastName;
MyDate dateOfBirth;
}
And I would like to turn a Lad
object into a JSON string like this:
{
"firstName":"Markoff",
"lastName":"Chaney",
"dateOfBirth":
{
"year":"1901",
"month":"4",
"day":"30"
}
}
(without the formatting). I found this link, but it uses a namespace that's not in .NET 4. I also heard about JSON.NET, but their site seems to be down at the moment, and I'm not keen on using external DLL files. Are there other options besides manually creating a JSON string writer?
Related Questions
Sponsored Content
65 Answered Questions
22 Answered Questions
[SOLVED] Convert JS object to JSON string
- 2010-11-12 08:20:50
- Bin Chen
- 1521256 View
- 1204 Score
- 22 Answer
- Tags: javascript json string object
26 Answered Questions
[SOLVED] How can I pretty-print JSON using JavaScript?
- 2011-01-26 22:33:53
- Mark
- 941721 View
- 2241 Score
- 26 Answer
- Tags: javascript json pretty-print
23 Answered Questions
[SOLVED] How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
- 2011-08-24 08:51:11
- kamaci
- 2648117 View
- 2623 Score
- 23 Answer
- Tags: json rest spring-mvc curl http-headers
61 Answered Questions
24 Answered Questions
[SOLVED] Safely turning a JSON string into an object
- 2008-09-05 00:12:01
- Matt Sheppard
- 1230639 View
- 1306 Score
- 24 Answer
- Tags: javascript json
54 Answered Questions
[SOLVED] How can I pretty-print JSON in a shell script?
- 2008-12-09 08:20:45
- AnC
- 1163053 View
- 2925 Score
- 54 Answer
- Tags: json unix command-line format pretty-print
39 Answered Questions
[SOLVED] How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
- 2009-01-23 13:39:54
- Agnel Kurian
- 1142376 View
- 2131 Score
- 39 Answer
- Tags: c# .net string character-encoding
26 Answered Questions
[SOLVED] Deserialize JSON into C# dynamic object?
- 2010-06-29 16:04:01
- jswanson
- 677032 View
- 924 Score
- 26 Answer
- Tags: c# .net json serialization dynamic
14 comments
@tdykstra 2019-10-08 21:26:32
A new JSON serializer is available in the
System.Text.Json
namespace. It's included in the .NET Core 3.0 shared framework and is in a NuGet package for projects that target .NET Standard or .NET Framework or .NET Core 2.x.Example code:
In this example the classes to be serialized have properties rather than fields; the
System.Text.Json
serializer currently doesn't serialize fields.Documentation:
@Waleed Naveed 2019-08-02 08:16:43
You can achieve this by using Newtonsoft.json. Install Newtonsoft.json from Nuget. and then:
@Darin Dimitrov 2011-06-01 13:05:38
You could use the
JavaScriptSerializer
class (add reference toSystem.Web.Extensions
):A full example:
@rzelek 2016-02-01 15:12:31
Please have in mind that Microsoft suggests to use JSON.net instead of this solution. I think that this answer became inappropriate. Take a look at willsteel's answer. Source: https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx.
@Mafii 2016-07-06 10:19:43
@DarinDimitrov you should consider adding a hint about JSON.net. Microsoft recommends it over JavascriptSerializer: msdn.microsoft.com/en-us/library/… You could also add a hint to msdn.microsoft.com/en-us/library/… which is the framework included approach
@stom 2017-01-25 08:24:58
here is online tool to convert your
classes
tojson
format, hope helps someone.@Happy Bird 2017-02-13 15:31:24
using System.Web.Script.Serialization;
@Protector one 2017-03-17 09:53:22
Why would Microsoft recommend a 3rd party solution over their own? Their wording is very odd as well: "Json.NET should be used serialization and deserialization. Provides serialization and deserialization functionality for AJAX-enabled applications."
@Hossein Ganjyar 2018-04-16 08:32:32
but this solution not return number as string... please tell me a solution same of javascript stringify, that it convert any key:value to string. my problem is with number.
@whiskeyfur 2018-08-10 16:57:07
@rzlek Microsoft only recommended JSON.net for one specific version of the .net library (If you believe the butchered english that is). That recommendation did NOT carry over after that, and the link posted is no longer being maintained.
@Sisir 2018-10-15 09:18:32
Just a heads up, to reference to
System.Web.Extensions
, you must haveASP.NET AJAX 1.0
orASP.NET 3.5
installed on your system. Please see this stackoverflow.com/questions/7723489/…@Artem Polishchuk 2014-11-21 22:18:58
Use this tools for generate C# class, then use this code to serialize your object
@user8426627 2018-01-28 16:00:31
If they are not very big, whats probably your case export it as Json. Also this makes portable among all plattforms
@Gokulan P H 2017-06-21 08:15:20
Use Json.Net library, you can download it from Nuget Packet Manager.
Serializing to Json String:
Deserializing to Object:
@micahhoover 2016-09-16 15:53:46
If you are in an ASP.NET MVC web controller it's as simple as:
Can't believe no one has mentioned this.
@csga5000 2016-09-23 05:30:31
I get an error about not being able to cast jsonresult to string.
@ewomack 2016-11-07 19:55:20
It will compile with implicit typing: var ladAsJson = Json(Lad).
@Edgar 2011-06-01 13:03:02
Use the
DataContractJsonSerializer
class: MSDN1, MSDN2.My example: HERE.
It can also safely deserialize objects from a JSON string, unlike
JavaScriptSerializer
. But personally I still prefer Json.NET.@Cristi Diaconescu 2015-01-17 21:26:05
Still don't see any examples on that page, but here are some on MSDN and elsewhere -> the last one uses extension methods to achieve one-liners.
@Cristi Diaconescu 2015-01-19 08:48:54
Oh, I missed the 2nd MSDN link :)
@MiFreidgeim SO-stop being evil 2016-07-14 02:56:32
It doesn't serialize plain classes. The error reported "Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute."
@Edgar 2016-07-15 06:25:32
@MichaelFreidgeim That's right, you have to mark properties in the class you want to serialize with attributes. DataContractAttribute DataMemberAttribute
@MiFreidgeim SO-stop being evil 2016-07-15 07:14:19
@Edgar: this is why JavaScriptSerializer better :)
@Edgar 2016-07-15 13:49:14
@MichaelFreidgeim Which is better depends on the requirements. The attributes let you configure how the property is serialized.
@amuliar 2017-11-13 11:58:49
DataContractJsonSerializer is very bad because it requires attributes to be set for any type (e.g. library objects).
@wchoward 2016-02-03 18:00:09
Serializer
Object
Implementation
Output
@Jean J. Michel 2014-02-04 20:07:38
Wooou! Really better using a JSON framework :)
Here is my example using Json.NET (http://james.newtonking.com/json):
The test:
The result:
Now I will implement the constructor method that will receives a JSON string and populates the class' fields.
@MatthewD 2016-05-30 13:33:12
Good post, this is the most current way to do it.
@MarzSocks 2014-06-26 14:02:01
As easy as this, works for dynamic objects as well (type object):
@Mahdi Rafatjah 2016-07-11 07:17:23
there is no default script under web. :(
@MarzSocks 2016-07-11 19:08:41
You are looking for this:msdn.microsoft.com/en-us/library/…
@Mahdi Rafatjah 2016-07-12 03:43:45
I kind of tried that but no. Script I guess I should add it as reference. So thanks a lot
@mschmoock 2013-10-02 12:39:47
Since we all love one liners
... this one depends on the Newtonsoft NuGet package, which is popular and better than the default serializer.
Documentation: Serializing and Deserializing JSON
@Andrei 2013-10-02 13:04:01
Newtonsoft serializer is way faster and mor customizable then built in. Highly recommend to use it. Thanks for the answer @willsteel
@Josef Pfleger 2015-06-01 08:53:50
It's worth pointing out that it's not really free. See /pricing on the website.
@David Cumps 2015-06-03 19:51:01
@JosefPfleger the pricing is for JSON.NET Schema, not JSON.NET the regular serializer, which is MIT
@nemke 2015-09-28 11:34:17
My testing showed that Newtonsoft is slower than JavaScriptSerializer class. (.NET 4.5.2)
@dsghi 2015-11-05 06:10:48
If you read the MSDN documentation for JavaScriptSerializer, it flat out says use JSON.net.
@cb88 2016-08-24 15:28:52
@JosefPfleger Newtionsoft JSON.net is MIT licensed... you could make modifications and resell it it you wanted. Their pricing page is about commercial technical support, and some schema validator they have.
@Stamos 2017-03-30 10:09:18
With
JavaScriptSerializer
DateTime property was serialized as"date": "/Date(1490648400000)/"
and was causing deserialized date to be a day back .InNewtonsof
was"date": "2017-03-29T00:00:00"
and solved my problem.@Barry Guvenkaya 2017-07-06 01:55:03
ExpandoObject could be the way to go if you guys want to create a dynamic object.
@Hithesh 2014-03-06 09:39:37
Use the below code for converting XML to JSON.
@James 2013-10-02 13:39:59
I would vote for ServiceStack's JSON Serializer:
It is also the fastest JSON serializer available for .NET: http://www.servicestack.net/benchmarks/
@Michael Logutov 2013-10-25 09:22:44
Those are very old benchmarks there. I've just test all three current versions of Newtonsoft, ServiceStack and JavaScriptSerializer and currently Newtonsoft is the fastest. Tho they all do quite fast.
@joelnet 2013-12-19 00:25:51
ServiceStack doesn't appear to be free.
@James 2013-12-19 10:21:18
@joelnet this is now the case, but was free when answering the question. However it is free for small sites, and I am still using it even though it is paid, it is a superb framework.
@JohnLBevan 2018-12-13 09:39:36
Some benchmarks here, though there's non for the serialization on its own: docs.servicestack.net/real-world-performance