Questions tagged [javascriptserializer]

System.Web.Script.Serialization.JavaScriptSerializer is a class which provides JSON serialization and deserialization functionality for applications and web sites targetting the .NET Framework. Use this tag for questions about this specific class only. For more general JavaScript/JSON serialization questions use the [json] and [serialisation] tags.

System.Web.Script.Serialization.JavaScriptSerializer is a class which provides JSON serialization and deserialization functionality for applications and web sites targetting the .NET Framework.

Use this tag for questions about the Microsoft class. For more general JSON serialization questions use both the and tags.

MSDN Remarks

The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server. You cannot access that instance of the serializer. However, this class exposes a public API. Therefore, you can use the class when you want to work with JavaScript Object Notation (JSON) in managed code.

To serialize an object, use the Serialize method. To deserialize a JSON string, use the Deserialize or DeserializeObject methods. To serialize and deserialize types that are not natively supported by JavaScriptSerializer, implement custom converters by using the JavaScriptConverter class. Then register the converters by using the RegisterConverters method.

Example

public abstract class AsSerializable
{
 public string AsJson()
 {
  var serializer = new JavaScriptSerializer();
  return serializer.Serialize(this);
 }
}

public class Foo : AsSerializable
{
 public string Bar { get; set; }
}

string JSON = Foo.AsJson();

MSDN Article

  • JavaScriptSerializer

    JavaScriptSerializer Code

  • Code

    Performance Benchamrks

  • http://james.newtonking.com/archive/2008/10/27/json-net-3-5-beta-1-big-performance-improvements-compact-framework-support-and-more.aspx

  • http://www.servicestack.net/benchmarks/
  • 420 questions
    3
    votes
    1 answer

    serializing C# objects with JavaScriptSerializer produces invalid bytes in UTF8 string

    I am serializing a C# object with the following code: var serializer = new JavaScriptSerializer(); var serializedResult = serializer.Serialize(rawFile); Console.WriteLine(serializedResult); …
    witek
    • 984
    • 1
    • 8
    • 25
    3
    votes
    2 answers

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

    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 =…
    Monojit Sarkar
    • 2,353
    • 8
    • 43
    • 94
    3
    votes
    1 answer

    How to serialize a property which is decorated with the [ScriptIgnore] attribute?

    I'm trying to serialize an object which has some properties with the [ScriptIgnore] attribute. However, I sometimes want the JavaScriptSerializer to not ignore properties with that attribute. Are there any possibilities to serialize the whole object…
    majstor
    • 379
    • 3
    • 14
    3
    votes
    1 answer

    Serialize and Deserialize Multidimensional Array to JSON

    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…
    ykh
    • 1,775
    • 3
    • 31
    • 57
    3
    votes
    2 answers

    why can't JavaScriptSerializer serialize internal properties?

    I've been serializing custom type which has some internal properties but when serializing, it seems that using System.Web.Script.Serialization.JavaScriptSerializer serialize method do not serialize internal properties (as it skips the internal…
    Akash KC
    • 16,057
    • 6
    • 39
    • 59
    3
    votes
    2 answers

    Can't deserialize an array of objects from JSON into a C# object

    I'm having trouble getting the JavascriptSerializer to deserialize an array of objects. I'm not terribly sure what I'm missing. Here's my object... public class HomefinderResult { public Data data; public List listings; public…
    SomeGuy1989
    • 483
    • 4
    • 8
    3
    votes
    2 answers

    JavaScriptSerializer and monodevelop

    Im reading this book but I JavaScriptSerializer from the System.Web.Script.Serialization namespace because it seems to be unavailable?
    marcus
    • 9,616
    • 9
    • 58
    • 108
    3
    votes
    1 answer

    Deserialize JSON with C# (Multiple Objects)

    I'm trying to deserialize a School to then insert into a Bar Chart Later. The JSON object looks like: [{"Subject": "TEST APP","AppScores": [{"Season": "AAAAAAAAAAAAAAAAAAAA","year": "1"}, {"Season": "BBBBBBBBBBBBBBBBBBBBB","year": "2"}]},…
    3
    votes
    1 answer

    How do I use JavaScriptSerializer in ASP.NET 5 RC 1?

    In earlier versions of ASP.NET, System.Web.Script.Serialization.JavaScriptSerializer was available. usage: Dictionary sample = (Dictionary)new JavaScriptSerializer().DeserializeObject("..."); In ASP.NET 5 RC 1,…
    3
    votes
    2 answers

    JSON deserialize to class with missing key in json [string could be a single string or list string]

    I have below class [Serializable] public class filters { public List key1 { get; set; } public List key2 { get; set; } public List key3 { get; set; } } and json string is [{"key1":…
    Md. Parvez Alam
    • 4,326
    • 5
    • 48
    • 108
    3
    votes
    1 answer

    How to extract information from an Object produced by JavaScriptSerializer

    I'm developing an application in C# which can control a SqueezeboxServer(SBS). Communicating to the SBS is via JSON messages to http://serverIP:9000/jsonrpc.js So I send JSON messages via a HTTPWepRequest and get answers via an HTTPWebResponse. The…
    user341877
    • 237
    • 2
    • 12
    3
    votes
    1 answer

    How do I use JavaScriptSerializer in ASP.NET 5?

    I am porting my project to DNX-Core 5.0 and trying to get work but I cannot find the JavaScriptSerializer and AppSettingReader classes. I know the System.Web is removed and so please anyone help me to find the alternative of using them. Is there…
    3
    votes
    2 answers

    Convert vb.net class object to JSON string in vb.net

    I am a c# developer and have the requirement to work in vb.net project. I am facing a simple issue I need to convert a class object to json string in vb.net.Problem is when I check the string after conversion I am getting output as: [{},{},{}] I am…
    killer
    • 592
    • 1
    • 9
    • 31
    3
    votes
    1 answer

    C# JavaScriptSerializer - how to serialize as base type?

    I have a problem serializing with JavaScriptSerializer. For example, I have a base class public class MyBaseClass{ public int Id { get; set; } public string Name { get; set; } } and a derived class public class MyDerivedClass : MyBaseClass…
    3
    votes
    1 answer

    String exceeds maxJsonLength with less than 250kb

    I'm using an entity together with ajax. I want the full table i provice with Entity framework ina grid created with JavaScript. The table I'm currently sending has less than 140 lines. My code works if I only have 50 lines in the table a few more an…
    Freddy
    • 960
    • 1
    • 20
    • 46