json.net

Returning a JSON stream from an ApiController

自作多情 提交于 2020-01-14 13:54:11
问题 I've got a large JSON Object which I need to return from an MVC ApiController. I can't simply return the JSON Object as the internal serializer barfs on the serialized contents being too long. I don't want to serialize to string and then return the string as we could be dealing 5-6MB of data and that seems like a waste of memory for no purpose. What I want to do is use the Newtonsoft JSON Serializer to write to the response stream directly. Dim Ret = Client.Search(Model) ''Get the results

Ignore property when serializing under certain conditions

≡放荡痞女 提交于 2020-01-14 12:54:09
问题 Json.net has a very useful property that can be set on the JsonPropertyAttribute called NullValueHandling . If you decorate a property like this: [JsonProperty(NullValueHandling=NullValueHandling.Ignore)] string MyProp { get; set; } Then if MyProp is null, it won't be included in the output at all. I would like to do something similar, but with a different condition for exclusion. For example - say we have an enum public enum MyEnum { None = 0, Value1 = 1, Value2 = 2, Value3 = 4 }; And a

Ignore property when serializing under certain conditions

南楼画角 提交于 2020-01-14 12:52:26
问题 Json.net has a very useful property that can be set on the JsonPropertyAttribute called NullValueHandling . If you decorate a property like this: [JsonProperty(NullValueHandling=NullValueHandling.Ignore)] string MyProp { get; set; } Then if MyProp is null, it won't be included in the output at all. I would like to do something similar, but with a different condition for exclusion. For example - say we have an enum public enum MyEnum { None = 0, Value1 = 1, Value2 = 2, Value3 = 4 }; And a

Retrieve required string from bigger JSON string

谁都会走 提交于 2020-01-14 12:36:54
问题 I have JSON string returned by method: public List<Issues> Getdata( string jql, List<string> fields = null, int startAt = 0, int maxResult = 500) { string data = JsonConvert.SerializeObject(request); string result = runQuery(JiraResource.search, data, "POST"); //returns results and working fine SearchResponse response = JsonConvert.DeserializeObject<SearchResponse>(result); return response.IssueDescriptions = GetIssues(response);; } and JSON string look like this: {"expand":"schema,names",

Retrieve required string from bigger JSON string

泄露秘密 提交于 2020-01-14 12:35:45
问题 I have JSON string returned by method: public List<Issues> Getdata( string jql, List<string> fields = null, int startAt = 0, int maxResult = 500) { string data = JsonConvert.SerializeObject(request); string result = runQuery(JiraResource.search, data, "POST"); //returns results and working fine SearchResponse response = JsonConvert.DeserializeObject<SearchResponse>(result); return response.IssueDescriptions = GetIssues(response);; } and JSON string look like this: {"expand":"schema,names",

Loading a JSON file containing JSON within brackets

流过昼夜 提交于 2020-01-14 10:23:54
问题 I'm trying to load a JSON file on a website using C# and JSON.Net However, I'm running into a problem when it runs because all the JSON is within []. Here's the JSON: [{"embed_count":"16","name":"live_user_catreina","stream_count":"133","category":"gaming","format":"live","channel_count":272,"title":"SWTOR - Sith Marauder - L42 - Belsavis - The Fatman","featured":true,"site_count":"117","abuse_reported":false,"channel":{"image_url_large":"http://static-cdn.jtvnw.net/jtv_user_pictures/catreina

Deserialize ISO 8601 date time string to C# DateTime

拟墨画扇 提交于 2020-01-14 09:08:33
问题 I'm trying to use: JsonConvert.DeserializeObject<DateTime>( "2009-02-15T00:00:00Z", new IsoDateTimeConverter() ) But it gives me a FormatException: Input string was not in a correct format. What am I doing wrong? 回答1: If you're parsing a single value, the simplest approach is probably to just use DateTime.ParseExact: DateTime value = DateTime.ParseExact(text, "o", null); The "o" pattern is the round-trip pattern, which is designed to be ISO-8601: The "O" or "o" standard format specifier

Parse JSON without declaring model class

我与影子孤独终老i 提交于 2020-01-14 05:21:10
问题 I have JSON like this: { "bookings": { "group_id": "abc", "name": "Study Rooms", "url": "My URL", "timeslots": [{ "room_id": "bcd", "room_name": "101", "booking_label": "Meeting1", "booking_start": "2018-11-30T07:00:00-06:00", "booking_end": "2018-11-30T07:30:00-06:00", "booking_created": "2018-11-28T11:32:32-06:00" }, { "room_id": "cde", "room_name": "102", "booking_label": "Meeting2", "booking_start": "2018-11-30T07:30:00-06:00", "booking_end": "2018-11-30T08:00:00-06:00", "booking_created"

How to determine which constructor Autofac uses when resolving

孤者浪人 提交于 2020-01-14 03:15:22
问题 I'm using a custom JsonConverter and JsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects to create the required instances during deserialization. The instances are created by resolving the types from an Autofac IOC container. Everything works fine, except... I have several "core objects" that request a unique Id in the constructor from a service (which is correctly injected into the constructor). When deserializing this should not happen because it is fairly expensive and the

Jquery DataTable passing a parameter to ajax call asp.net. Invalid JSON primitive

不打扰是莪最后的温柔 提交于 2020-01-13 20:31:52
问题 Below is my java script code to Populate my HTML table with server data and I am using Jquery DataTables to server this purpose. function LoadData(result) { $('#example').DataTable({ "ajax": { "dataType": 'json', "contentType": "application/json; charset=utf-8", "type": "POST", "url": "index.aspx/Risky", "data": function (d) { return JSON.stringify( d ) //return JSON.stringify(result); // d.extra_search = result; //"extra_search": result }, "dataSrc": function (json) { return $.parseJSON(json