Questions tagged [json.net]

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

json.net
Filter by
Sorted by
Tagged with
149 votes
9 answers
62k views

How to handle both a single item and an array for the same property using JSON.net

I'm trying to fix my SendGridPlus library to deal with SendGrid events, but I'm having some trouble with the inconsistent treatment of categories in the API. In the following example payload taken ...
Robert McLaws's user avatar
48 votes
3 answers
7k views

How can I parse a JSON string that would cause illegal C# identifiers?

I have been using NewtonSoft JSON Convert library to parse and convert JSON string to C# objects. But now I have came across a really awkward JSON string and I am unable to convert it into C# object ...
TaLha Khan's user avatar
  • 2,423
116 votes
6 answers
72k views

Deserializing polymorphic json classes without type information using json.net

This Imgur api call returns a list containing both Gallery Image and Gallery Album classes represented in JSON. I can't see how to deserialize these automatically using Json.NET given that there is ...
Peter Kneale's user avatar
  • 1,926
340 votes
9 answers
382k views

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 ...
Snakebyte's user avatar
  • 3,775
859 votes
22 answers
1.0m views

How can I deserialize JSON to a simple Dictionary<string,string> 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 ...
richardtallent's user avatar
504 votes
9 answers
497k views

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(...
ryudice's user avatar
  • 36.9k
37 votes
2 answers
26k views

TypeNameHandling caution in Newtonsoft Json

On this link, in remarks section it's mentioned that: TypeNameHandling should be used with caution when your application deserializes JSON from an external source. Incoming types should be ...
donkey's user avatar
  • 513
209 votes
7 answers
195k views

Parse JSON in C#

I'm trying to parse some JSON data from the Google AJAX Search API. I have this URL and I'd like to break it down so that the results are displayed. I've currently written this code, but I'm pretty ...
Mike B's user avatar
  • 12.8k
530 votes
4 answers
440k views

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 = ...
culix's user avatar
  • 10.4k
621 votes
27 answers
519k views

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 ...
NevenHuynh's user avatar
  • 6,679
10 votes
1 answer
3k views

Create a strongly typed c# object from json object with ID as the name

I am trying to make use of the API for a well known online meeting provider. One of their API calls returns an object that looks like this. { "5234592":{ "pollsAndSurveys":{ "...
kaelle's user avatar
  • 467
36 votes
1 answer
10k views

Does Json.NET cache types' serialization information?

In .NET world, when it comes to object serialization, it usually goes into inspecting the object's fields and properties at runtime. Using reflection for this job is usually slow and is undesirable ...
KFL's user avatar
  • 17.5k
199 votes
6 answers
185k views

JSON.net: how to deserialize without using the default constructor?

I have a class that has a default constructor and also an overloaded constructor that takes in a set of parameters. These parameters match to fields on the object and are assigned on construction. ...
kmacdonald's user avatar
  • 3,421
723 votes
17 answers
641k views

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 ...
Amit's user avatar
  • 25.7k
333 votes
12 answers
667k views

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's user avatar
173 votes
5 answers
398k views

Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly

I have this JSON: [ { "Attributes": [ { "Key": "Name", "Value": { "Value": "Acc 1", "Values": [ ...
Avinash's user avatar
  • 2,143
147 votes
5 answers
380k views

Deserializing JSON data to C# using JSON.NET

I'm relatively new to working with C# and JSON data and am seeking guidance. I'm using C# 3.0, with .NET3.5SP1, and JSON.NET 3.5r6. I have a defined C# class that I need to populate from a JSON ...
user avatar
61 votes
2 answers
66k views

Setting the default JSON serializer in ASP.NET MVC

I'm working on an existing application that has been partially converted over to MVC. Whenever a controller responds with a JSON ActionResult, the enums are sent as numbers opposed to the string name....
John's user avatar
  • 17.4k
8 votes
3 answers
13k views

JSON.NET Parser *seems* to be double serializing my objects

My problem is this: This is the response being sent back from my WebAPI controller. "[ [ {\"id\":\"identifier\"}, {\"name\":\"foobar\"} ] ]" Notice that the response is wrapped in ...
K. Alan Bates's user avatar
369 votes
6 answers
353k views

.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":"...
RasikaSam's user avatar
  • 5,473
112 votes
5 answers
165k views

Parsing JSON using Json.net

I'm trying to parse some JSON using the JSon.Net library. The documentation seems a little sparse and I'm confused as to how to accomplish what I need. Here is the format for the JSON I need to ...
Carter's user avatar
  • 2,850
69 votes
7 answers
43k views

Can I specify a path in an attribute to map a property in my class to a child property in my JSON?

There is some code (which I can't change) that uses Newtonsoft.Json's DeserializeObject<T>(strJSONData) to take data from a web request and convert it to a class object (I can change the class). ...
David P's user avatar
  • 2,027
57 votes
5 answers
36k views

How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?

I need to deserialize a complex JSON blob into standard .NET containers for use in code that is not aware of JSON. It expects things to be in standard .NET types, specifically Dictionary<string, ...
dongryphon's user avatar
22 votes
3 answers
22k views

Json.Net Serialization of Type with Polymorphic Child Object

We would like to be able to serialize/deserialize json from/to C# classes, with the main class having an instance of a polymorphic child object. Doing so is easy using Json.Net's TypeNameHandling.Auto ...
GaTechThomas's user avatar
  • 5,794
2 votes
3 answers
4k views

How to deserialize an array of values with a fixed schema to a strongly typed data class?

I am having some trouble figuring out a clean (as possible) way to deserialize some JSON data in a particular format. I want to deserialize the data to strongly typed data object classes, pretty ...
Patrick's user avatar
  • 148
343 votes
12 answers
748k views

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 ...
J Benjamin's user avatar
  • 4,752
199 votes
7 answers
153k views

Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?

I've just upgraded my ASP web API project from .NET Core 2.0 to 3.0. I was using services.AddMvc() .AddJsonOptions(opts => opts.SerializerSettings.ContractResolver = new ...
NeilMacMullen's user avatar
18 votes
2 answers
11k views

Line delimited json serializing and de-serializing

I am using JSON.NET and C# 5. I need to serialize/de-serialize list of objects into line delimited json. http://en.wikipedia.org/wiki/Line_Delimited_JSON. Example, {"some":"thing1"} {"some":"thing2"} ...
Imran Qadir Baksh - Baloch's user avatar
190 votes
7 answers
269k views

Can Json.NET serialize / deserialize to / from a stream?

I have heard that Json.NET is faster than DataContractJsonSerializer, and wanted to give it a try... But I couldn't find any methods on JsonConvert that take a stream rather than a string. For ...
Omri Gazitt's user avatar
  • 3,468
143 votes
6 answers
156k views

Json.net serialize/deserialize derived types?

json.net (newtonsoft) I am looking through the documentation but I can't find anything on this or the best way to do it. public class Base { public string Name; } public class Derived : Base { ...
Will's user avatar
  • 10.2k
44 votes
4 answers
131k views

Deserialize JSON to nested C# Classes

Below is a (slightly) stripped down response I get from a REST API upon successful creation of a new "job code" entry. I need to deserialize the response into some classes, but I'm stumped. ...
Jake Bullet's user avatar
7 votes
5 answers
12k views

JSON.Net throws StackOverflowException when using [JsonConvert()]

I wrote this simple code to Serialize classes as flatten, but when I use [JsonConverter(typeof(FJson))] annotation, it throws a StackOverflowException. If I call the SerializeObject manually, it ...
Rasool Ghafari's user avatar
215 votes
5 answers
580k views

How to write a JSON file in C#?

I need to write the following data into a text file using JSON format in C#. The brackets are important for it to be valid JSON format. [ { "Id": 1, "SSN": 123, "Message": "whatever" ...
user1429595's user avatar
  • 2,685
73 votes
5 answers
66k views

Detect if deserialized object is missing a field with the JsonConvert class in Json.NET (Newtonsoft)

I'm trying to deserialize some JSON objects using Json.NET. I've found however that when I deserialize an object that doesn't have the properties I'm looking for that no error is thrown up but a ...
DubiousPusher's user avatar
81 votes
5 answers
70k views

Deserialize json with known and unknown fields

Given following json result: The default json result has a known set of fields: { "id": "7908", "name": "product name" } But can be extended with ...
nickvane's user avatar
  • 2,989
59 votes
5 answers
86k views

How to parse huge JSON file as stream in Json.NET?

I have a very, very large JSON file (1000+ MB) of identical JSON objects. For example: [ { "id": 1, "value": "hello", "another_value": "world", "value_obj": { ...
fdmillion's user avatar
  • 4,964
189 votes
17 answers
209k views

Casting interfaces for deserialization in JSON.NET

I am trying to set up a reader that will take in JSON objects from various websites (think information scraping) and translate them into C# objects. I am currently using JSON.NET for the ...
tmesser's user avatar
  • 7,606
103 votes
7 answers
76k views

Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?

Is it possible to use JSON.NET as default JSON serializer in ASP.NET MVC 3? According to my research, it seems that the only way to accomplish this is to extend ActionResult as JsonResult ...
zam6ak's user avatar
  • 7,229
78 votes
5 answers
90k views

how to deserialize JSON into IEnumerable<BaseType> with Newtonsoft JSON.NET

given this JSON: [ { "$id": "1", "$type": "MyAssembly.ClassA, MyAssembly", "Email": "[email protected]", }, { "$id": "2", "$type": "MyAssembly.ClassB, MyAssembly", "Email": "...
Andrew Bullock's user avatar
45 votes
3 answers
28k views

How to serialize a Dictionary as part of its parent object using Json.Net

I'm using Json.Net for serialization. I have a class with a Dictionary: public class Test { public string X { get; set; } public Dictionary<string, string> Y { get; set; } } Can I ...
Nataly's user avatar
  • 553
8 votes
1 answer
17k views

Deserializing JSON into an object

I have some JSON: { "foo" : [ { "bar" : "baz" }, { "bar" : "qux" } ] } And I want to deserialize this into a collection. I have defined this class: public class Foo { ...
CodeCaster's user avatar
  • 150k
208 votes
8 answers
264k views

Specifying a custom DateTime format when serializing with Json.Net

I am developing an API to expose some data using ASP.NET Web API. In one of the API, the client wants us to expose the date in yyyy-MM-dd format. I don't want to change the global settings (e.g. ...
Stay Foolish's user avatar
  • 3,686
81 votes
9 answers
456k views

Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

Here is my simple User POCO class: /// <summary> /// The User class represents a Coderwall User. /// </summary> public class User { /// <summary> /// A User's username. eg: "...
Only Bolivian Here's user avatar
82 votes
3 answers
143k views

How to set custom JsonSerializerSettings for Json.NET in ASP.NET Web API?

I understand that ASP.NET Web API natively uses Json.NET for (de)serializing objects, but is there a way to specify a JsonSerializerSettings object that you want for it to use? For example, what if ...
Richard Neil Ilagan's user avatar
3 votes
2 answers
3k views

How to deserialize a child object with dynamic (numeric) key names?

how can I deserialize below json structure using newtonsoft json.net in .net. { "users" : { "parentname":"test", "100034" : { "name" : "tom", "state" : "...
amit's user avatar
  • 33
93 votes
9 answers
115k views

Using Json.NET converters to deserialize properties

I have a class definition that contains a property that returns an interface. public class Foo { public int Number { get; set; } public ISomething Thing { get; set; } } Attempting to ...
dthrasher's user avatar
  • 41.3k
62 votes
7 answers
38k views

Deserializing JSON when sometimes array and sometimes object

I'm having a bit of trouble deserializing data returned from Facebook using the JSON.NET libraries. The JSON returned from just a simple wall post looks like: { "attachment":{"description":""}, ...
mfanto's user avatar
  • 14.3k
184 votes
13 answers
123k views

Making a property deserialize but not serialize with json.net

We have some configuration files which were generated by serializing C# objects with Json.net. We'd like to migrate one property of the serialised class away from being a simple enum property into a ...
Will Dean's user avatar
  • 39.3k
15 votes
1 answer
15k views

What is the correct way to use JSON.NET to parse stream of JSON objects?

I have a stream of JSON objects that looks somewhat like this: {...}{...}{...}{...}... So basically a concatenated list of JSON objects without any separator. What's the proper way to deserialize ...
Andrey Shchekin's user avatar
267 votes
46 answers
490k views

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 ...
user3437755's user avatar
  • 2,671

1
2 3 4 5
45