Questions tagged [json.net]
Json.NET (also known as Newtonsoft.Json) is a popular high-performance JSON framework for .NET.
json.net
13,957
questions
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 ...
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 ...
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 ...
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 = ...
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(...
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":"...
366
votes
3
answers
306k
views
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)
...
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 ...
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 ...
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?
327
votes
6
answers
362k
views
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
}
...
324
votes
14
answers
227k
views
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 ...
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 ...
267
votes
37
answers
535k
views
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:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture=...
219
votes
13
answers
307k
views
How to make sure that string is valid JSON using JSON.NET
How can one validate whether a raw string is valid JSON or just text? I'm using JSON.NET.
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"
...
214
votes
13
answers
233k
views
JSON.Net Self referencing loop detected
I have a mssql database for my website within 4 tables.
When I use this:
public static string GetAllEventsForJSON()
{
using (CyberDBDataContext db = new CyberDBDataContext())
{
...
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 ...
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. ...
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 ...
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. ...
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 ...
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 ...
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 ...
177
votes
22
answers
912k
views
Unexpected character encountered while parsing value
Currently, I have some issues. I'm using C# with Json.NET. The issue is that I always get:
{"Unexpected character encountered while parsing value: e. Path '', line 0, position 0."}
So the ...
176
votes
16
answers
115k
views
Order of serialized fields using JSON.NET
Is there a way to specify the order of fields in a serialized JSON object using JSON.NET?
It would be sufficient to specify that a single field always appear first.
173
votes
5
answers
397k
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": [
...
165
votes
5
answers
116k
views
What exceptions does Newtonsoft.Json.DeserializeObject throw?
What exceptions does Newtonsoft.Json.DeserializeObject throw? I want to handle them.
http://james.newtonking.com/json/help/?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObject.htm#...
158
votes
4
answers
158k
views
Specify a value can be a string or null with JSON Schema
Hopefully this isn't obvious to others because I find the docs at https://json-schema.org/ to be lacking in finer details. I'm getting a block of JSON with some properties that can be null or a string....
157
votes
4
answers
144k
views
json.net has key method?
If my response has key "error" I need to process error and show warning box.
Is there "haskey" method exists in json.net?
Like:
var x= JObject.Parse(string_my);
if(x.HasKey["error_msg"])
...
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 ...
149
votes
7
answers
304k
views
Deserializing JSON Object Array with Json.net
I am attempt to use an API that use the follow example structure for their returned json
[
{
"customer":{
"first_name":"Test",
"last_name":"Account",
"email":"...
148
votes
6
answers
215k
views
Get value from JToken that may not exist (best practices)
What's the best practice for retrieving JSON values that may not even exist in C# using Json.NET?
Right now I'm dealing with a JSON provider that returns JSON that sometimes contains certain key/...
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 ...
146
votes
4
answers
186k
views
How do I enumerate through a JObject?
I'm trying to determine how to access the data that is in my JObject and I can't for the life of me determine how to use it.
JObject Object = (JObject)Response.Data["my_key"];
I can print it to the ...
143
votes
3
answers
189k
views
How to serialize a JObject without the formatting?
I have a JObject (I'm using Json.Net) that I constructed with LINQ to JSON (also provided by the same library). When I call the ToString() method on the JObject, it outputs the results as formatted ...
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
{
...
140
votes
8
answers
218k
views
Creating JSON on the fly with JObject
For some of my unit tests I want the ability to build up particular JSON values (record albums in this case) that can be used as input for the system under test.
I have the following code:
var ...
135
votes
13
answers
155k
views
How to store JSON in an entity field with EF Core?
I am creating a reusable library using .NET Core (targeting .NETStandard 1.4) and I am using Entity Framework Core (and new to both). I have an entity class that looks like:
public class Campaign
{
...
131
votes
9
answers
337k
views
How to import JsonConvert in C# application?
I created a C# library project. The project has this line in one class:
JsonConvert.SerializeObject(objectList);
I'm getting error saying
the name JsonConvert doesn't exist in the current ...
127
votes
5
answers
346k
views
Send JSON via POST in C# and Receive the JSON returned?
This is my first time ever using JSON as well as System.Net and the WebRequest in any of my applications. My application is supposed to send a JSON payload, similar to the one below to an ...
125
votes
6
answers
207k
views
Checking for empty or null JToken in a JObject
I have the following...
JArray clients = (JArray)clientsParsed["objects"];
foreach (JObject item in clients.Children())
{
// etc.. SQL params stuff...
command.Parameters["@MyParameter"]....
123
votes
4
answers
177k
views
Convert object of any type to JObject with Json.NET
I often need to extend my Domain model with additional info before returning it to the client with WebAPI. To avoid creation of ViewModel I thought I could return JObject with additional properties. I ...
118
votes
5
answers
137k
views
Convert JObject into Dictionary<string, object>. Is it possible?
I have a web API method that accepts an arbitrary json payload into a JObject property. As such I don't know what's coming but I still need to translate it to .NET types. I would like to have a ...
116
votes
9
answers
105k
views
Web API 2: how to return JSON with camelCased property names, on objects and their sub-objects
UPDATE
Thanks for all the answers. I am on a new project and it looks like I've finally got to the bottom of this: It looks like the following code was in fact to blame:
public static ...
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 ...
114
votes
5
answers
67k
views
Private setters in Json.Net
I know there's an attribute to handle private setters but I kind of want this behavior as a default, is there a way to accomplish this? Except tweaking the source. Would be great if there was a ...
113
votes
6
answers
80k
views
Ensuring json keys are lowercase in .NET
Is there simple way using JSON in .NET to ensure that the keys are sent as lower case?
At the moment I'm using the newtonsoft's Json.NET library and simply using
string loginRequest = JsonConvert....
112
votes
2
answers
142k
views
Newtonsoft JsonSerializer - Lower case properties and dictionary [duplicate]
I'm using json.net (Newtonsoft's JsonSerializer). I need to customize serialization in order to meet following requirements:
property names must start with lower case letter.
Dictionary<string, T&...
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 ...