javascriptserializer

JavaScriptSerializer - how to deserialize a property with a dash (“-”) in it's name?

扶醉桌前 提交于 2019-12-18 04:59:10
问题 Trying to deserialize this JSON: { "result":"success" "arguments": { "activeTorrentCount":22, "cumulative-stats": { "downloadedBytes":1111, } } } My class: private class DeserializationMain { public string result; //works public args arguments; //works, has deserialized activeTorrentCount public class args { public int activeTorrentCount; public current cumulative_stats; //doesn't work, equals null public class current { public long downloadedBytes; } } } I guess cumulative-stats doesn't get

convert json to c# list of objects

点点圈 提交于 2019-12-17 18:55:59
问题 Json string: {"movies":[{"id":"1","title":"Sherlock"},{"id":"2","title":"The Matrix"}]} C# class: public class Movie { public string title { get; set; } } C# converting json to c# list of Movie's: JavaScriptSerializer jss = new JavaScriptSerializer(); List<Movie> movies = jss.Deserialize<List<Movie>>(jsonString); My movies variable is ending up being an empty list with count = 0. Am I missing something? 回答1: Your c# class mapping doesn't match with json structure. Solution : class

ASP.NET WebService is Wrapping my JSON response with XML tags

痴心易碎 提交于 2019-12-17 11:45:12
问题 I'm not sure where I'm going wrong of what I'm missing. I'm building an ASP.NET 2.0 (on the .Net 3.5 framework) Web application and I am including a webservice. Note that this is not an MVC project. I wish to expose a method which will return a JSON string; formatted to feed the jqGrid jQuery plugin. This is the preliminary test method I've implemented in my service: thanks to (Phil Haack's Guide for MVC) [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getData()

Asmx web service how to return JSON and not XML?

余生长醉 提交于 2019-12-17 06:56:29
问题 My service method: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string getDataFromTrainingMaster() { List<TrainingMasterDataStruct> results = new DAL().GetDataFromTrainingMaster(); JavaScriptSerializer js = new JavaScriptSerializer(); return js.Serialize(results).ToString(); } My .net web service returns JSON wrapped in XML as follows: <?xml version="1.0" encoding="UTF-8"?> <string xmlns="http://tempuri.org/"> [{"Training_Code":"1234 ","Training_Duration":"2hrs ",

Create json using JavaScriptSerializer

陌路散爱 提交于 2019-12-13 08:26:06
问题 I have following code in Generic Handler of Asp.net: var students= Student.GetStudents(); var result = new { Data = students.Select(s => new []{s.subjects}) }; var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var json = serializer.Serialize(result); context.Response.ContentType = "application/json"; context.Response.Write(json); Applied on the Class Student : public class Student { public List<String> subjects{get; set;} Student() { subjects= new List<string>(); }

Serializing double value to JSON string

空扰寡人 提交于 2019-12-13 04:41:41
问题 I'm trying to serialize a double value 1.0 into JSON value 1.0. However, the following code outputs 1 instead of 1.0: var jsSerializer = new JavaScriptSerializer(); var json = jsSerializer.Serialize(1.0); Console.WriteLine(json); // actual: 1, expected: 1.0 Does anyone has any idea how to accomplish this? I though of creating a custom converter for double types by extending JavaScriptConverter , but the problem is the Serialize method is supposed to return IDictionary<string, object> . 回答1:

Can't use .NET's JavaScriptSerializer in Unity and MonoDevelop on MacOS

一笑奈何 提交于 2019-12-12 18:30:48
问题 I'm trying to use .NET's System.Web.Script.Serialization.JavaScriptSerializer to read a JSON file into a Dictionary in Unity with MonoDevelop on MacOS. So far I've added the System.Web.Extensions assembly to MonoDevelop which makes it build the "Assembly-CSharp" project fine. But when I go back to Unity, it still shows the error: error CS0234: The type or namespace name `Web' does not exist in the namespace `System'. Are you missing an assembly reference? Then I read on the web that adding

jQuery JSON Posting: Invalid object passed in, ':' or '}' expected

…衆ロ難τιáo~ 提交于 2019-12-12 15:24:13
问题 When I am trying to post JSON to server side function then I am getting this error Invalid object passed in, ':' or '}' expected I am working ckeditor and this way I am getting data from ckeditor. var ckEditorCtrl = GetClientID("CKEditor1").attr("id"); var newcontent = getEditorContents(ckEditorCtrl.toString()); function GetClientID(id, context) { var el = $("#" + id, context); if (el.length < 1) el = $("[id$=_" + id + "]", context); return el; } function getEditorContents(ename) { if

Serialize and Deserialize Multidimensional Array to JSON

佐手、 提交于 2019-12-11 05:02:02
问题 I am using the built-in library JavaScriptSerializer to serialize and deserialize a multidimensional array. It is stated in the MSDN that A multidimensional array is serialized as a one-dimensional array, and you should use it as a flat array. I have tried to deserialize it, but kept getting Unable to cast object of type 'System.Double[]' to type 'System.Double[,,]'. double[, ,] y = serializer.Deserialize<double[, ,]>(jsonMatrix); Does this mean that i have re-construct it back manually ? If

Serialize and deserialize Dictionary<int, object> using JavaScriptSerializer and a custom JavaScriptConverter

爷,独闯天下 提交于 2019-12-11 02:52:27
问题 How would you serialize and deserialize a dictionary whose keys are integers to JSON, using a JavaScriptSerializer and a custom JavaScriptConverter? For those who don't know, the JavaScripSerializer cannot do this out of the box. Please note that I'm not interested in a solution that requires converting the dictionary prior to serialization or that uses another serializer (if you are, you may see this post). UPDATE : to remove any ambiguity, I have no problem with the fact the key is