json.net

JSON.Net - DeserializeObject Format

和自甴很熟 提交于 2019-12-05 18:21:26
I'm using JSON.Net to try and deserialize some survey responses from SurveyGizmo. Here's a snapshot of the data I'm reading in: {"result_ok":true, "total_count":"44", "page":1, "total_pages":1, "results_per_page":50, "data":[ {"id":"1", "contact_id":"", "status":"Complete", "is_test_data":"0", "datesubmitted":"2011-11-13 22:26:53", "[question(59)]":"11\/12\/2011", "[question(60)]":"06:15 pm", "[question(62)]":"72", "[question(63)]":"One", "[question(69), option(10196)]":"10", I've setup a class as far as datesubmitted but I'm not sure how to setup the class to deserialize the questions given

MVC4: Include object name in Json

时间秒杀一切 提交于 2019-12-05 17:54:33
I have a question on a MVC4 (beta) issue I have been struggling with for some time now. The issue is that I want to create a json with the object name added for my webapi. The json has to be created this was as the receiving side needs this. My .NET/MVC knowledge is limited so please bear with me. I tried searching on the subject, but as MVC4 is still in beta it's difficult to find good info on this subject. I have already imported the JSON.NET formatter in my solution, but this does not add the Object name The json that is now created in MVC4: [{"ID":36,"Name":"Test3","Description":

Sanitizing Input with JsonConvert.SerializeObject in MVC4?

ぃ、小莉子 提交于 2019-12-05 17:43:14
Long story short, I'm trying to get the output from JsonConvert.SerializeObject to be sanitized without having to modify the contents of the saved data. I'm working on an app that has the following markup in the view: <textarea data-bind="value: aboutMe"></textarea> If I save the following text, I run into problems: <script type="text/javascript">alert("hey")</script> The error I get in FF: The relevant part of the offending rendered text: $(document).ready(ko.applyBindings(new MyProfileVm({"profileUsername":"admin","username":"Admin","aboutMe":"alert(\"hey\")","title":"Here's a short self-bio

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

会有一股神秘感。 提交于 2019-12-05 17:20:35
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, the above creates the following json { 'one': 'Un', 'two': 'Deux', 'three': 'Trois' } How can I

How to deserialize with JSON.NET?

ε祈祈猫儿з 提交于 2019-12-05 17:12:42
How do I setup Newtonsoft Json.net to deserialize this text into a .NET object? [ [ "US\/Hawaii", "GMT-10:00 - Hawaii" ], [ "US\/Alaska", "GMT-09:00 - Alaska" ], ] For bonus points, what is this kind of structure called in Json. I tried looking for anonymous objects, but didn't have any luck. Stuart JSON.Net uses JArray to allow these to be parsed - see: Deserializing variable Type JSON array using DataContractJsonSerializer (the questions is about DataContract - but the answer is about JSON.Net - and is from JSON.Net's author) Json.NET: serializing/deserializing arrays This JSON string (or

How to detect duplicate keys in Web Api Post request Json

混江龙づ霸主 提交于 2019-12-05 16:58:17
I have a requirement to return a 400 error from an ASP.NET Web API Post request when the request Json contains duplicate keys. For instance if the request was { "key1": "value1", "key2": 1000, "key2": 2000, "key3": "value3" } then I would want the error to be thrown due to there being two "key2" keys. My controller method looks something like [HttpPost] public IHttpActionResult PostMethod([FromBody]RequestModel request) { ..... } and my RequestModel model like public class RequestModel { [Required] public string Key1 {get; set; } [Required] public int Key2 {get; set; } public string Key3 {get;

Serializing to JSON, Nullable Date gets omitted using Json.NET and Web API despite specifying NullValueHandling

纵饮孤独 提交于 2019-12-05 15:54:11
using Newtonsoft.Json; namespace FAL.WebAPI2012.Controllers { public class Person { public int Id {get;set;} public string FirstName {get;set;} public string LastName {get;set;} [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include, NullValueHandling = NullValueHandling.Include)] public DateTime? Dob { get; set; } } public class TestNullsController : ApiController { // GET api/<controller> public Person Get() { Person myPerson = new Person() { Dob = null, FirstName = "Adrian", Id=1, LastName="Bobby" }; return myPerson; } } } As you can see, my Dob field is set to null but the

Getting “because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly” error when deserializing a Json object

拜拜、爱过 提交于 2019-12-05 14:23:42
Please help me. Where I am missing info? I need to deserialize the following JSON string. {"results":[{"series":[{"name":"PWR_00000555","columns":["time","last"],"values":[["1970-01-01T00:00:00Z",72]]}]}]} For this, I have defined my class: public class Serie { public Serie() { this.Points = new List<object[]>(); } [JsonProperty(PropertyName = "results")] public string results { get; set; } [JsonProperty(PropertyName = "series")] public string sries { get; set; } [JsonProperty(PropertyName = "name")] public string Name { get; set; } [JsonProperty(PropertyName = "columns")] public string[]

How do you parse a JSON file using JSON.net

[亡魂溺海] 提交于 2019-12-05 13:51:42
问题 I am trying to read a JSON file and parse it. I have this code for reading from my file StreamReader re = new StreamReader("artists.json"); JsonTextReader reader = new JsonTextReader(re); But how do I parse it now from reader so I can search data from the file? I tried reading the documentation but couldn't find anything 回答1: using Newtonsoft.Json; //.. JsonSerializer se = new JsonSerializer(); object parsedData = se.Deserialize(reader); 回答2: If you want to load it into a JObject or a dynamic

Deserializing a list of objects with different names in JSON.NET

▼魔方 西西 提交于 2019-12-05 13:36:28
I'm getting my data from a website which returns a .json format that is quite unfamiliar to me. I've been looking for the solution for a couple of hours, and I must be using the terminology. The json is formatted something like this: [ { "Foo": { "name": "Foo", "size": { "human": "832.73kB", "bytes": 852718 }, "date": { "human": "September 18, 2017", "epoch": 1505776741 }, } }, { "bar": { "name": "bar", "size": { "human": "4.02MB", "bytes": 4212456 }, "date": { "human": "September 18, 2017", "epoch": 1505776741 } } }] I'm using Newtonsoft's JSON.NET, and I can't seem to be able to create a