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
    10
    votes
    2 answers

    Convert type 'System.Dynamic.DynamicObject to System.Collections.IEnumerable

    I'm successfully using the JavaScriptSerializer in MVC3 to de-serialize a json string in to a dynamic object. What I can't figure out is how to cast it to something I can enumerate over. The foreach line of code below is my latest attemt but it…
    user1842828
    • 311
    • 3
    • 9
    • 17
    10
    votes
    3 answers

    JavaScriptSerializer with custom Type

    I have a function with a List return type. I'm using this in a JSON-enabled WebService like: [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List GetProducts(string dummy) /* without…
    balint
    • 3,391
    • 7
    • 41
    • 50
    9
    votes
    1 answer

    Escaping quotes with JavaScriptSerializer in ASP.NET MVC3

    I Have an Array of users that i want to pass into a view as a javascript array. I'm doing this via JavaScriptSerializer, but the string i get has unescaped quotes in it. Controller Action public virtual ActionResult Create(int id) { …
    MrBliz
    • 5,830
    • 15
    • 57
    • 81
    9
    votes
    2 answers

    JavaScriptSerializer - custom property name

    I am using JavaScriptSerializer to deserialize json data. Everything works pretty well, but my problem is, that one property in json data is named 'base', so I cannot create such property in my C# code. I found that i can manually map values to…
    Eleer
    • 598
    • 3
    • 6
    • 20
    8
    votes
    2 answers

    Using JavaScriptSerializer.DeserializeObject how can I get back a Dictionary that uses a case insensitive string comparer?

    I have some JSON that I need to deserialize so I'm using JavaScriptSerializer.DeserializeObject like: var jsonObject = serializer.DeserializeObject(line) as Dictionary; The problem is that the Dictionary that comes back has a…
    Kang Su
    • 701
    • 2
    • 10
    • 19
    8
    votes
    2 answers

    How to decode a JSON string using C#?

    I'm looking for an example code/lib to decode a JSON string using C#. To encode I can do this: var data = new Dictionary(); data.Add("..", "..."); var json_encoded = new JavaScriptSerializer().Serialize(data); but how do I…
    The Mask
    • 17,007
    • 37
    • 111
    • 185
    8
    votes
    1 answer

    JSON deserialize , Error : null to value type, how to know exact property causing error?

    In my C# code, I'm trying to deserialize a JSON with 100s of properties (complex, primitive, derived) and I'm getting an error Cannot convert null to a value type. Though I finally knew which property is causing a problem by manual…
    anoop
    • 3,812
    • 2
    • 16
    • 28
    8
    votes
    1 answer

    Json circular reference in powershell 2.0 with javascriptSerializer

    I´m writing a script in powershell 2.0 and an upgrade to 3.0 or higher is not possible right now. In this script I try to serialize some data to JSON with the code from this link (PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json…
    8
    votes
    5 answers

    ASP.NET C#: JavascriptSerializer could not be found

    I'm trying to use the JavascriptSerializer object in ASP.NET v4.0 with C#. I'm not using Visual Studio--this is on a live IIS7 server. I can access this object just fine using VB on this same web server, so I know the requisite DLLs are present…
    Octavient
    • 603
    • 3
    • 11
    • 21
    7
    votes
    2 answers

    javascriptserializer date format issue

    I am serializing a complex object with lot of properties of other Types and Lists to JSON form but the issue is with DateTime properties. I get the epoch time with JavascriptSerializer (rather than mm/dd/YYYY). Is there any way I can get the…
    Subhasis
    • 714
    • 3
    • 14
    • 28
    7
    votes
    1 answer

    Web.config jsonSerialization maxJsonLength ignored

    I have an MVC3 application running under .NET 4.0 and when I use JavascriptSerializer.Deserialize I'm getting an error. Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value…
    Zach L
    • 1,277
    • 4
    • 18
    • 37
    7
    votes
    1 answer

    Using System.Web.Script.Serialization.JavascriptSerializer to deserialize JSON - how to?

    Note: I posted a similar question, which was the ancestor of this question, as I was originally thinking of using JSON.NET to parse the JSON, but I'm using the built-in deserializer, so it's a different question. This is what I'm trying to do: I…
    Maxim Zaslavsky
    • 17,787
    • 30
    • 107
    • 173
    6
    votes
    1 answer

    ASMX webservice - return JSON instead of XML

    I have a web service that contains one method: [WebMethod] public string Movies() { using (var dataContext = new MovieCollectionDataContext()) { var query = dataContext.Movies.Select(m =>new{m.Title,m.ReleaseDate}).Take(20); …
    Haseeb Khan
    • 930
    • 3
    • 19
    • 41
    6
    votes
    1 answer

    Return JSON from ASMX web service, without XML wrapper?

    I need to get Json data from a C# web service. I know there are several questions based on this, trust me I have read through quite a few but only to confuse me further. This is what I have done : In my web service I have included :…
    Praneeta
    • 1,554
    • 3
    • 19
    • 20
    6
    votes
    2 answers

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

    Trying to deserialize this JSON: { "result":"success" "arguments": { "activeTorrentCount":22, "cumulative-stats": { "downloadedBytes":1111, } } } My class: …
    Lars
    • 1,699
    • 2
    • 22
    • 32
    1 2
    3
    27 28