json.net

JSON.Net JsonConverter for DbGeography

三世轮回 提交于 2019-12-30 00:38:25
问题 Long long struggles with this... Basically I have a model-first EF5 object with a DbGeography property. I would like to apply a JsonConverter that let's it roundtrip as simple latitude/longitude values. I'm using WebAPI. Looking for JSON output and input like so: { "location": { "geopoint": { "latitude":40.770712, "longitude":-73.962011 } } } Here is my class definition and JsonConverter: [MetadataType(typeof(QueryLocationMetadata))] partial class Location { } public class

如何将JSON反序列化为简单的Dictionary <string,string> 在ASP.NET中?

独自空忆成欢 提交于 2019-12-29 17:18:24
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我在JSON中有一个简单的键/值列表,该列表通过POST发送回ASP.NET。 例: { "key1": "value1", "key2": "value2"} 我不打算将序列化为严格类型的.NET对象 我只需要一个普通的旧 Dictionary(Of String,String) 或某些等效项(哈希表,Dictionary(Of String,Object),老式的StringDictionary-地狱,二维字符串数组对我来说就可以工作。 我可以使用ASP.NET 3.5以及流行的Json.NET(我已经在使用它序列化 到 客户端)中使用的任何东西。 显然,这两个JSON库都不具有这种开箱即用的明显功能-它们完全专注于通过强合同进行基于反射的反序列化。 有任何想法吗? 局限性: 我不想实现自己的JSON解析器 尚不能使用ASP.NET 4.0 希望远离JSON不再使用的旧的ASP.NET类 #1楼 十分烦人的是,如果您要使用默认的模型绑定程序,则看起来您将不得不使用数字索引值(如POST表单)。 请参阅本文的以下摘录 http://msdn.microsoft.com/zh-cn/magazine/hh781022.aspx : 尽管有点违反直觉,但JSON请求具有相同的要求-它们也必须遵守表单后命名语法。

Unable to cast object of type Newtonsoft.Json.Linq.JObject even though I am trying to cast to an object with matching properties

人走茶凉 提交于 2019-12-29 09:56:30
问题 I am working with ASP.NET Core 2.0 in VS2017. I am trying to deserialize some JSON that is returned in an HttpResponseMessage but I am getting an "Unable to cast object of type..." exception. Here is the code that is failing; FilesUploadedListResponse fileUploadListResponse = new FilesUploadedListResponse(); string jsonResult = response.Content.ReadAsStringAsync().Result; fileUploadListResponse = (FilesUploadedListResponse)JsonConvert.DeserializeObject(jsonResult); The last line is where I

posting a JObject to an action

心不动则不痛 提交于 2019-12-29 09:33:11
问题 I was reading Rick Strahls http://www.west-wind.com/weblog/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods I would like to be able to use JObject in to the the action. I wrote a controller like this public class AlbumsController : Controller { [System.Web.Http.HttpPost] public string PostAlbum(JObject jsonData) { return "success"; } } the front end looks like this when the ajax gets called I get an error $("#a").click(function () { var album = { AlbumName:

Deserializing JSON to flattened class

喜你入骨 提交于 2019-12-29 09:20:11
问题 I found the same question here... Deserializing nested JSON structure to a flattened class with Json.NET using annotations ...but without a proper answer. One of the best suggestion is to wrap the nested object in a new class but this approach introduces another issue: lego name. In my example the most logic name for this class is the same name that parent class and of course is not possible. My example is simple, I just want to eliminate the "language" property in the parent class. Can

C# Deserialize an object which is derived and has references

北城余情 提交于 2019-12-29 09:13:08
问题 I have a object of Type Node. Node.cs The serialization works when I make the call as following: var nodeSer = JsonConvert.SerializeObject(mynode, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }); My problem is that the following call does not work. var n = JsonConvert.DeserializeObject<Node>(nodeSer, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling =

Proper way of using Newtonsoft Json ItemConverterType

爱⌒轻易说出口 提交于 2019-12-29 08:46:11
问题 I have some bad data coming back from a web service which I cannot change. The service returns a JSON list of customers. Inside this list, each customer also has a list of jobs. But the JSON coming back is a string for the jobs. So: Jobs: "[]" Instead of Jobs: [] So I defined the class as [JsonProperty(PropertyName = "JOBS", ItemConverterType = typeof(StringToJobsConverter))] public List<JobClass> Jobs { get; set; } I created the class, and created the conversion method inside it as follows:

Why can I not deserialize this custom struct using Json.Net?

佐手、 提交于 2019-12-29 08:36:28
问题 I have a struct representing a DateTime which also has zone info as below: public struct DateTimeWithZone { private readonly DateTime _utcDateTime; private readonly TimeZoneInfo _timeZone; public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, DateTimeKind kind = DateTimeKind.Utc) { dateTime = DateTime.SpecifyKind(dateTime, kind); _utcDateTime = dateTime.Kind != DateTimeKind.Utc ? TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone) : dateTime; _timeZone = timeZone; } public DateTime

Using JSON.NET to serialize object into HttpClient's response stream

本秂侑毒 提交于 2019-12-29 08:12:25
问题 Abstract Hi, I'm working on a project where it is needed to send potentially huge json of some object via HttpClient, a 10-20 mb of JSON is a typical size. In order do that efficiently I want to use streams, both with Json.Net to serialize an object plus streams for posting data with HttpClient. Problem Here is the snippet for serialization with Json.net, in order to work with streams, Json.net expects a stream that it will write into: public static void Serialize( object value, Stream

Using JSON.NET to serialize object into HttpClient's response stream

杀马特。学长 韩版系。学妹 提交于 2019-12-29 08:12:08
问题 Abstract Hi, I'm working on a project where it is needed to send potentially huge json of some object via HttpClient, a 10-20 mb of JSON is a typical size. In order do that efficiently I want to use streams, both with Json.Net to serialize an object plus streams for posting data with HttpClient. Problem Here is the snippet for serialization with Json.net, in order to work with streams, Json.net expects a stream that it will write into: public static void Serialize( object value, Stream