json.net

Handling json pretty print param in ASP.NET Web API application in HttpConfiguration instance

淺唱寂寞╮ 提交于 2019-12-24 04:43:05
问题 I need to add and handle optional "pretty" parameter in my ASP.NET Web API application. When user sends "pretty=true", the application response should look like a human-readable json with indentations. When user sends "pretty=false" or does not send this parameter at all, he must get json with no space symbols in response. Here's what I have: Global.asax.cs public class WebApiApplication : HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas();

Is there a better way to handle Encoding values with Json.Net?

一世执手 提交于 2019-12-24 02:26:14
问题 I am serializing/deserializing a class that has a property of type System.Text.Encoding with Json.Net. Trying out a simple test, my class serialized without any issue: public class TestClass { public Encoding TheEncoding { get; set; } } var testClass = new TestClass { TheEncoding = Encoding.UTF8 }; var json = JsonConvert.SerializeObject( testClass, Formatting.Indented ); var obj = JsonConvert.DeserializeObject<TestClass>( json ); Serializes to: { "TheEncoding": { "BodyName": "utf-8",

Is there a better way to handle Encoding values with Json.Net?

徘徊边缘 提交于 2019-12-24 02:26:12
问题 I am serializing/deserializing a class that has a property of type System.Text.Encoding with Json.Net. Trying out a simple test, my class serialized without any issue: public class TestClass { public Encoding TheEncoding { get; set; } } var testClass = new TestClass { TheEncoding = Encoding.UTF8 }; var json = JsonConvert.SerializeObject( testClass, Formatting.Indented ); var obj = JsonConvert.DeserializeObject<TestClass>( json ); Serializes to: { "TheEncoding": { "BodyName": "utf-8",

json.net custom jobject deserialization

旧城冷巷雨未停 提交于 2019-12-24 01:53:57
问题 I'm trying to use JsonConvert.DeserializeObject(string) to deserialize a string into an jobject that can use with dynamic to access the json document on the fly. However I want to avoid knowing the casing of the document so I can type dynamic document = JsonConvert.DeserializeObject(someString); Console.WriteLine(document.some.path.here.name); and have it work on {"Some":{"path":{"HERE":{"Name":"test"}}} I can't find out how to create a custom class for json.net that will do that for me,

Deserializing anything using JSON.NET

£可爱£侵袭症+ 提交于 2019-12-24 01:44:16
问题 I have no issue deserializing JSON into known types or into Dictionary objects, but what about cases where I don't know what the input is going to be? Specifically I'm referring to receiving a JSON string that represents a flat or nested set of key-value pairs: { foo: 'bar', baz: 42 } or { foo: { bar: 42, baz: ['foo', 'bar', 'baz'] } } But what about cases where the input isn't a key-value-pair, but rather an array, or, an array of objects with other nested objects (including arrays)? [ { foo

Parse Google Maps Geocode API Using Json.Net

假装没事ソ 提交于 2019-12-24 01:24:58
问题 I thought I could simply build a quick class and use JSON.Net to deserialize the result, and it is kind of working but I think I am blowing it on my class structure: public class Location { public string lat { get; set; } public string lng { get; set; } } public class Geometry { public Location location { get; set; } } public class Json { public Results results { get; set; } } public class Results { public Json json { get; set; } public Geometry geometry { get; set; } } public class Query {

Deserialize part of json to datatable in C# using JSON.NET

天大地大妈咪最大 提交于 2019-12-24 01:15:04
问题 This is probably very simple and a basic lack of understanding on my part. I am calling an API which returns a JSON as below: { disclaimer: "https://openexchangerates.org/terms/", license: "https://openexchangerates.org/license/", timestamp: 1449877801, base: "USD", rates: { AED: 3.672538, AFN: 66.809999, ALL: 125.716501, AMD: 484.902502, ANG: 1.788575, AOA: 135.295998, ARS: 9.750101, AUD: 1.390866, } } All I want to do is return the result into a datatable which has the columns Timestamp,

c# get values from json

╄→гoц情女王★ 提交于 2019-12-24 00:47:14
问题 I have a json text and i want to get the values of author name and description tags. no need of other fields like url and urltoimage and all. when i run the below code does not providing any string values. i think some error goes here. { "status": "ok", "articles": [ { "source": { "id": "techcrunch", "name": "TechCrunch" }, "author": "Khaled \"Tito\" Hamze", "title": "Crunch Report", "description": "Your daily roundup of the biggest TechCrunch stories and startup news.", "url": "https:/

How can I deserialize instances of a type that has read-only back-references to some container type also being deserialized?

可紊 提交于 2019-12-24 00:29:28
问题 Let's say I have two types, a Document and a Child . The Child is nested fairly deeply within the Document , and contains a back-reference to the parent that must needs be passed into its constructor. How can I deserialize such an object graph with Json.NET and pass the parent into the child's constructor? Here's a concrete example, inspired by Pass constructor arguments when deserializing into a List(Of T) by Ama: Class Document Public Property MyObjects as List(Of Child) = new List(Of Child

Json.net deserialising to objects from JSon that may contain either an array, or a nested array

泄露秘密 提交于 2019-12-24 00:23:50
问题 I have a number of JSon files that I'm deserialising using JsonSerializer serializer = new JsonSerializer(); t obj = (t)serializer.Deserialize(file, typeof(t)); to a collection of objects. They contain, amongst other things the following data. The number of arrays in "truthtable" is determined by the value of "gates" "gates" : 1, "truthtable" : [ false, true ] and "gates" : 2, "truthtable" : [ [ false, false ], [ false, true ] ] if I try to deserialise "truthtable" to the following property,