json.net

JSON.Net deserialize string which contains special characters

限于喜欢 提交于 2019-12-10 10:23:11
问题 How do I parse JSON string with one of the values containing special characters? JObject obj = JObject.Parse(str); str value: { "message": "some !@#$%^&*(){}:"?/?/|"':>;><{"d":"v"}" } I have got execption: After parsing a value an unexpected character was encountered: {. 回答1: Take your JSON and .stringify() it. { "message": JSON.stringify("your text here") } If you have raw data in your ASP.NET MVC view, you can follow this way: { "message": JSON.stringify("@Html.Raw(HttpUtility

Why is my HttpWebRequest POST method to my WebAPI server failing?

百般思念 提交于 2019-12-10 10:22:48
问题 I've successfully received data from my WebAPI project ("GET"), but my attempt to Post is not working. Here is the relevant server/WebAPI code: public Department Add(Department item) { if (item == null) { throw new ArgumentNullException("item"); } departments.Add(item); return item; } ...which fails on the "departments.Add(item);" line, when this code from the client is invoked: const string uri = "http://localhost:48614/api/departments"; var dept = new Department(); dept.Id = 8; dept

Parse Json Array objects using Newtonsoft.Json

 ̄綄美尐妖づ 提交于 2019-12-10 10:08:20
问题 I have an array of objects like this in json as per below format { Table: [ { userstatus: [ { STATUS: "TRUE", PACK: "UM6MONTHPACK", EXPIRY: "8/15/2014 1:00:03 PM", } ] }, { activeauctions: [ { ISBILLED: "0", AUCTION_ID: "24", AUCTION_NAME: "Swimsuit", } ] }, { upcomingauctions: [ { AUCTION_ID: "4", AUCTION_NAME: "Jacqueline Fernandezs Handbag", SKU: "4_20131120" }, { AUCTION_ID: "4", AUCTION_NAME: "Jacqueline Fernandezs Handbag", SKU: "4_20131120" } ] } ] } I am deserializing like this: var

Newtonsoft.json IsoDateTimeConverter and DateFormatHandling

烈酒焚心 提交于 2019-12-10 09:59:08
问题 This is not an issue but concept I just want to make it clear. When working with asp.net web api we use Newtonsoft.Json's SerializerSettings . In this setting we can set jsonSetting.Converters.Add(new IsoDateTimeConverter()); jsonSetting.DateFormatHandling = DateFormatHandling.IsoDateFormat; I understood what IsoDateTimeConverter is doing but but not clear about DateFormatHandling.IsoDateFormat . It seems to be there is no effect of DateFormatHandling.MicrosoftDateFormat if you still using

How to create array of key/value pair in c#?

心已入冬 提交于 2019-12-10 09:24:18
问题 I have an application that is written on top of ASP.NET MVC. In one of my controllers, I need to create an object in C# so when it is converted to JSON using JsonConvert.SerializeObject() the results looks like this [ {'one': 'Un'}, {'two': 'Deux'}, {'three': 'Trois'} ] I tried to use Dictionary<string, string> like this var opts = new Dictionary<string, string>(); opts.Add("one", "Un"); opts.Add("two", "Deux"); opts.Add("three", "Trois"); var json = JsonConvert.SerializeObject(opts); However

NewtonSoft.Json custom JsonConverter deserialize to DateTime not working

扶醉桌前 提交于 2019-12-10 07:32:16
问题 I am trying to deserialize a Unix timestamp to a DateTime . In my case, I need to do much more checks before I can set a property to DateTime from a timestamp. If I use DateTime from Newtonsoft.Json it deserializes it to UTC time and I need to deserialize it to a specific timezone The problem is that I am not able to get the correct time. It seems like my string to long parsing is failing. If I can get the long unix timestamp, I can get the rest of the logic working I have a class named Alert

Serialize and deserialize HttpRequestMessage objects

不羁的心 提交于 2019-12-10 04:19:33
问题 Anyone have experience on serializing HttpRequestMessage objects? Trying with Json.net and it partially works. That said, JsonConvert.DeserializeObject fails due to constructure issues StringContent: "Unable to find a constructor to use for type System.Net.Http.StringContent". The use case here is in short that I want to save the web request and issue it later, in case of temporary network issues or service unavailability etc.. Example code that is failing: var request = new

Parse a Json Array in to a class in c#

帅比萌擦擦* 提交于 2019-12-10 04:18:50
问题 I parsed this single Json : { "text": "Sample Text", "id": 123456789, "user": { "name": "ExampleUser", "id": 123, "screen_name": "ExampleUser" }, "in_reply_to_screen_name": null, } to my c# class RootObject : public class User { public string name { get; set; } public int id { get; set; } public string screen_name { get; set; } } public class RootObject { public string text { get; set; } public long id { get; set; } public User user { get; set; } public object in_reply_to_screen_name { get;

How to get a string value from a JToken

笑着哭i 提交于 2019-12-10 03:56:29
问题 I'm getting data from a web service that returns a JSON response. This is my code: WebClient client = new WebClient(); var result = client.DownloadString("http://some url"); JObject obj = JObject.Parse(result); // Location l = new Location(); // l.city = obj["ad"][2]; error here At this point it returns a result, but I am getting an error: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JToken' to 'string' I would like some assistance getting the returned data into my variable in the

JSON.Net Struct Serialization Discrepancy

南楼画角 提交于 2019-12-10 03:05:56
问题 When using JSON.Net to serialize/deserialize structs, a build-in struct type (like System.Drawing.Size) serializes to a string, whereas a custom struct type serializes to a JSON object. For example: using System; using System.Drawing; using Newtonsoft.Json; namespace TestJsonNet { class Program { static void Main(string[] args) { Console.WriteLine(JsonConvert.SerializeObject(new Size(50, 50))); Console.WriteLine(JsonConvert.SerializeObject(new Size2(50, 50))); } } struct Size2 { public int