Questions tagged [json.net]

Json.NET (also known as Newtonsoft.Json) is a popular high-performance JSON framework for .NET.

Json.NET is a popular high-performance JSON framework for .NET.

The official website with documentation and examples.

Features

  • Flexible JSON serializer for converting between .NET objects and JSON
  • LINQ to JSON for manually reading and writing JSON
  • High performance, faster than .NET's built-in JSON serializers
  • Write indented, easy to read JSON
  • Convert JSON to and from XML
  • Supports .NET 2, .NET 3.5, .NET 4, .NET 4.5, Silverlight, Windows Phone, Windows 8 Store, and .NET Core with netstandard1.0.

The JSON serializer is a good choice when the JSON you are reading or writing maps closely to a .NET class.

LINQ to JSON is good for situations where you are only interested in getting values from JSON, you don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects.

The source code is hosted on GitHub. You can install Json.NET from NuGet:

PM> Install-Package Newtonsoft.Json
13615 questions
848
votes
22 answers

How can I deserialize JSON to a simple Dictionary in ASP.NET?

I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example: { "key1": "value1", "key2": "value2"} I AM NOT TRYING TO DESERIALIZE INTO STRONGLY-TYPED .NET OBJECTS I simply need a plain old Dictionary(Of String, String), or…
richardtallent
  • 34,724
  • 14
  • 83
  • 123
702
votes
17 answers

How to ignore a property in class if null, using json.net

I am using Json.NET to serialize a class to JSON. I have the class like this: class Test1 { [JsonProperty("id")] public string ID { get; set; } [JsonProperty("label")] public string Label { get; set; } [JsonProperty("url")] …
Amit
  • 25,106
  • 25
  • 75
  • 116
612
votes
27 answers

JSON.NET Error Self referencing loop detected for type

I tried to serialize POCO class that was automatically generated from Entity Data Model .edmx and when I used JsonConvert.SerializeObject I got the following error: Error Self referencing loop detected for type System.data.entity occurs. How do…
NevenHuynh
  • 6,589
  • 5
  • 22
  • 25
557
votes
19 answers

How can I deserialize JSON with C#?

I have the following code: var user = (Dictionary)serializer.DeserializeObject(responsecontent); The input in responsecontent is JSON, but it is not properly deserialized into an object. How should I properly deserialize it?
user605334
519
votes
4 answers

How can I change property names when serializing with Json.net?

I have some data in a C# DataSet object. I can serialize it right now using a Json.net converter like this DataSet data = new DataSet(); // do some work here to populate 'data' string output = JsonConvert.SerializeObject(data); However, this uses…
culix
  • 10,188
  • 6
  • 36
  • 52
497
votes
8 answers

Deserialize JSON object into dynamic object using Json.net

Is it possible to return a dynamic object from a json deserialization using json.net? I would like to do something like this: dynamic jsonResponse = JsonConvert.Deserialize(json); Console.WriteLine(jsonResponse.message);
ryudice
  • 36,476
  • 32
  • 115
  • 163
363
votes
6 answers

.NET NewtonSoft JSON deserialize map to a different property name

I have following JSON string which is received from an external party. { "team":[ { "v1":"", "attributes":{ "eighty_min_score":"", "home_or_away":"home", "score":"22", …
RasikaSam
  • 5,363
  • 6
  • 28
  • 36
358
votes
3 answers

How to deserialize a JObject to .NET object

I happily use the Newtonsoft JSON library. For example, I would create a JObject from a .NET object, in this case an instance of Exception (might or might not be a subclass) if (result is Exception) var jobjectInstance =…
Sebastian
  • 6,293
  • 6
  • 34
  • 47
340
votes
12 answers

Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

I know there are a few posts about Newtonsoft so hopefully this isn't exactly a repeat...I'm trying to convert JSON data returned by Kazaa's API into a nice object of some kind WebClient client = new WebClient(); Stream stream =…
J Benjamin
  • 4,722
  • 6
  • 29
  • 39
336
votes
9 answers

How to implement custom JsonConverter in JSON.NET?

I am trying to extend the JSON.net example given here http://james.newtonking.com/projects/json/help/CustomCreationConverter.html I have another sub class deriving from base class/Interface public class Person { public string FirstName { get;…
Snakebyte
  • 3,735
  • 3
  • 16
  • 12
328
votes
12 answers

How to convert JSON to XML or XML to JSON in C#?

I started to use Json.NET to convert a string in JSON format to object or viceversa. I am not sure in the Json.NET framework, is it possible to convert a string in JSON to XML format and viceversa?
David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
320
votes
6 answers

Convert Newtonsoft.Json.Linq.JArray to a list of specific object type

I have the following variable of type {Newtonsoft.Json.Linq.JArray}. properties["Value"] {[ { "Name": "Username", "Selected": true }, { "Name": "Password", "Selected": true } ]} What I want to accomplish is to convert this…
Mdb
  • 8,338
  • 22
  • 63
  • 98
315
votes
14 answers

How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?

My problem is that I wish to return camelCased (as opposed to the standard PascalCase) JSON data via ActionResults from ASP.NET MVC controller methods, serialized by JSON.NET. As an example consider the following C# class: public class Person { …
aknuds1
  • 65,625
  • 67
  • 195
  • 317
265
votes
46 answers

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

I am getting the Error System.IO.FileLoadException : Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition…
user3437755
  • 2,651
  • 2
  • 12
  • 3
261
votes
36 answers

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. Manifest definition does not match the assembly reference

Things I've tried after searching: in Web.Config put a binding on the old version:
noobieDev
  • 3,063
  • 3
  • 14
  • 14
1
2 3
99 100