json.net

Deserializing some JSON with JSON.NET

陌路散爱 提交于 2019-12-04 13:02:35
I'm very new to JSON, and I need to parse some that an API is providing. A quick google search turned up JSON.NET , so I'm trying to use it now to parse this JSON into a list object. First of all, is JSON.NET the best library to use for this? This is what I'm trying to do: I have a class called Item , for example. The json has many "elements" (if that's what they are called), and each contains 3 fields : an integer named id, a string named name, and a datetime named creationTime. I would like to parse all of these Item "elements" from the json into a list of Item objects . I have created 3

C# : extract/retrieve child node from JSON structure

橙三吉。 提交于 2019-12-04 12:56:31
How can we extract or retrieve child nodes values from JSON structure in C#. my app is using OpenWeatherMap , I need to retrieve name from city , temp from list and description from weather nodes, my JSON and Class structure are below { "cod": "200", "message": 0.0284, "city": { "id": 2643743, "name": "London", "coord": { "lon": -0.12574, "lat": 51.50853 }, "country": "GB", "population": 0, "sys": { "population": 0 } }, "cnt": 1, "list": [ { "dt": 1429268400, "temp": { "day": 12.21, "min": 4.86, "max": 13.18, "night": 4.86, "eve": 11.76, "morn": 12.21 }, "pressure": 1028.8, "humidity": 66,

Querying JSON with JSONPath or SelectTokens? With JSON.NET in C#

泪湿孤枕 提交于 2019-12-04 12:55:53
I am trying to use the Newtonsoft.Json.Net in c#. The following is part of JSON file that I need to retrieve data out of: { "video":{ "local_recording_device":{ "codecs":null }, "preferred_string":"___PREFERRED___", "streams":{ "99176901":{ "id":"99176901", "name":"PTZ Camera", "site":"someone", "email":"someone@awebsite.com", "codec":"VP8 HD1 (720p)", "local":true, "screen":false, "fit_to_window":true, "stay_on_top":false, "layout":0, "native_width":1280, "native_height":720, "window_width":456, "window_height":254, "preferred":false, "local_recording":false, "device_id":"MJPEG Camera",

How to parse a TimeSpan value in Newtonsoft JSON

帅比萌擦擦* 提交于 2019-12-04 12:20:12
I'd like parse JSON string and use the token.Type property to detect values of type JTokenType.TimeSpan . I can't work out how to express the TimeSpan in my input string, everything seems to be interpreted as JTokenType.String. var timeSpanString = TimeSpan.FromHours(1).ToString(); testString = string.Format(@"{{""Value"": ""{0}"" }}", timeSpanString); var statObject = JObject.Parse(testString); JToken token = statObject["Value"]; var tokenValue = token.ToString(); var tokenType = token.Type; // JTokenType.String I even tried: JValue jValue = new JValue("test"); jValue.Value = TimeSpan

How to parse nested JSON data structure

两盒软妹~` 提交于 2019-12-04 11:30:33
In a Windows Phone app I need to parse JSON data. I am able to get the values for keys which aren't nested. But if the JSON has arrays inside arrays, then how do I extract values from the JSON file? In the past what I did was parse the JSON into a JArray object, then from the JToken I got the value of a specified string key. In the JSON below, people has men and women , and men itself has many men with different IDs. So if I have this complete thing as a JSON string, how do I print the value of the ID of a particular man? I could have converted into JArray and access indexed values if there

Json.NET custom serialization/deserialization of a third party type

社会主义新天地 提交于 2019-12-04 11:24:27
I want to converts Vectors of the OpenTK library to and from JSON. The way I thought it worked is just making a custom JsonConverter, so I did this: class VectorConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(Vector4); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var obj = JToken.Load(reader); if (obj.Type == JTokenType.Array) { var arr = (JArray)obj; if (arr.Count == 4 && arr.All(token => token.Type == JTokenType.Float)) { return new Vector4(arr[0].Value<float

Using JObject and JProperty with JSON.Net 4.0

懵懂的女人 提交于 2019-12-04 11:00:20
I'm trying to deserialize JSON in this format: { "data": [ { "installed": 1, "user_likes": 1, "user_education_history": 1, "friends_education_history": 1, "bookmarked": 1 } ] } to a simple string array like this: { "installed", "user_likes", "user_education_history", "friends_education_history", "bookmarked" } using JSON.NET 4.0 I've gotten it to work using the `CustomCreationConverter' public class ListConverter : CustomCreationConverter<List<string>> { public override List<string> Create(Type objectType) { return new List<string>(); } public override object ReadJson(JsonReader reader, Type

Json.Net IsoDateTimeConverter is not working

百般思念 提交于 2019-12-04 10:54:23
I have a web application made in asp.net mvc 4 in Global.asax, i added added IsoDateTimeConverter protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add( new IsoDateTimeConverter { DateTimeFormat = "dd-MM-yyyy hh:mm" }); } I have an ActionResult public ActionResult GetDate() { DateTime dateTime = DateTime.Now; return Json(dateTime, JsonRequestBehavior.AllowGet); } However, this actionResult returns this: "\/Date(1365060823129)\/" What i am missing? Well I believe you got a little bit

How to get JSON.NET to serialize date/time to ISO 8601?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 10:22:25
问题 I have a Web API application that returns JSON to consumers who may not be using Microsoft technologies. When my controller returns an object with DateTime properties as JSON, it serializes the date in this format: 2017-03-15T00:00:00-04:00 This is giving the consumer a bit of a headache as they're expect it to be in ISO 8601 format. Some research has told me that JSON.NET now uses ISO 8601 by default (I am using 9.0.1). When I run this code... Clipboard.Copy(JsonConvert.SerializeObject

ASP.NET WebAPI JSON Binding Case-Sensitivity

谁说我不能喝 提交于 2019-12-04 10:02:59
问题 Upgrading from ASP.NET WebAPI Beta to RC has provided some amount of excitement and a great deal of frustration. I have been able to work through the majority of the issues, but the one that is biting me right now is case-sensitivity of JSON request data. The formatter used for JSON requests (which by default is a JSON.NET formatter) appears to be case sensitive while the formatter used for form-urlencoded data does not. Is there a way to configure the JSON requests to use a case-insensitive