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
    6
    votes
    4 answers

    Problem deserializating JSON Date in C# - adding 2 hours

    We are having such a nasty problem when deserializating a JSON date to a C# DateTime. The code is: JavaScriptSerializer serializer = new JavaScriptSerializer(); jsonTrechos = jsonTrechos.Replace("/Date(", "\\/Date(").Replace(")/", ")\\/"); Trecho[]…
    viniciushana
    • 235
    • 1
    • 2
    • 12
    6
    votes
    4 answers

    Invalid Json Primitives

    Could you help me for resolving this issue. I have one asp.net application, in this i am using Javascript serializer for serializing a dataset followed by convertion to the list. That code is shown below. JavaScriptSerializer json = new…
    MAC
    • 6,277
    • 19
    • 66
    • 111
    6
    votes
    2 answers

    JavaScriptSerializer deserialize object "collection" as property in object failing

    I have a js object structured like: object.property1 = "some string"; object.property2 = "some string"; object.property3.property1 = "some string"; object.property3.property2 = "some string"; object.property3.property2 = "some string"; i'm using…
    bill
    • 905
    • 5
    • 10
    • 21
    6
    votes
    3 answers

    VS 2013 Crash When Using JSON Deserialize Method

    I've created a C# ASP.NET method which simply takes a string property containing JSON, both inside the same class. My issue is that whenever I type the .Deserialize method (the commented line) Visual Studio 2013 crashes with an "Unhandled Exception"…
    Kallum Tanton
    • 802
    • 7
    • 22
    6
    votes
    1 answer

    JavascriptSerializer exception

    I have the following…
    pixartist
    • 1,137
    • 2
    • 18
    • 40
    6
    votes
    2 answers

    why does c# JavaScriptSerializer.Serialize return empty square brackets

    Why does the following code return "[ ]" when it should return "{"id":1999, "title":"hithere"} JavaScriptSerializer serializer = new JavaScriptSerializer(); StringBuilder sbJsonResults = new StringBuilder(); var result =…
    user2282496
    • 235
    • 4
    • 13
    6
    votes
    2 answers

    How do you serialize an enum array to a Json array of strings?

    Based on Diego's unanswered comment under the top-voted answer in this question: JSON serialization of enum as string So for an enum: public enum ContactType { Phone = 0, Email = 1, Mobile = 2 } And for eg. a property: //could contain…
    Mark Gibaud
    • 2,061
    • 23
    • 35
    6
    votes
    1 answer

    Why does web service return data as msg.d

    I found that my webmethod is returning data as { "d": [ {"id":"1","itemtxt":"Masters"}, {"id":"2","itemtxt":"Transactions"}, {"id":"3","itemtxt":"Misch. Reports"} ] } If you notice, the array is named as "d". Why is that ? is there any…
    Deb
    • 981
    • 13
    • 39
    5
    votes
    1 answer

    JavaScriptSerializer.Deserialize does not accept a date that was serialized by JavaScriptSerializer.Serialize

    How can I find the source of this problem, where a date value serialized by JavaScriptSerializer cannot be deserialized by the JavaScriptSerializer? In the calling application: var serializer = new JavaScriptSerializer(); var json =…
    JK.
    • 21,477
    • 35
    • 135
    • 214
    5
    votes
    2 answers

    Deserializing JSON string with JavaScriptSerializer to return a single dynamic object instead of an array of keys and values

    Consider the following JSON string in C#: { "BuyerRegion":"BuyerRegion [0][27]", "SellerRegion":"SellerRegion [0][29]", "HubRegion":"HubRegion [0][31]", "PartNo":"TINT_MNUM [0][3]", "BuyerCompID":"BuyerCompId [0][28]", …
    NewbieCoder
    • 676
    • 1
    • 9
    • 32
    5
    votes
    2 answers

    How to deal with JSON and variant objects?

    I'm using .NET's JavascriptSerializer to deserialize JSON into runtime objects and for the most part the mapping between JSON fields and object fields has been automatic. However, I am faced with the following scenario and would welcome advice on…
    I. J. Kennedy
    • 24,725
    • 16
    • 62
    • 87
    5
    votes
    2 answers

    Does Json.Encode() use JavaScriptSerializer Class to serialize

    Does the Json.Encode() Helper use the JavaScriptSerializer class to encode a string to json? I am getting a circular reference exception when using Json.Encode(Model) even though my class properties that are being serialized have the [ScriptIgnore]…
    bflemi3
    • 6,698
    • 20
    • 88
    • 155
    5
    votes
    1 answer

    Nested list with JavaScriptSerializer

    I'm trying to produce the following JSON structure with JavaScriptSerializer in Net 3.5: { "chart": "pie", "series": [ { "name": "Browsers", "data": [ [ "Firefox", …
    Tobias Nilsson
    • 512
    • 2
    • 13
    4
    votes
    1 answer

    How to serialize a EF POCO auto-generated object to avoid circular references?

    I'm using a library (Telerik) for ASP.NET MVC 3. I've a problem with all functionnalities which needs to return by AJAX some data: I'm using EF4 to connect my database, and I've(and need) navigation properties in both ways(Imagine, a User which have…
    J4N
    • 19,480
    • 39
    • 187
    • 340
    4
    votes
    1 answer

    JavaScriptSerializer and ValueTypes (struct)

    For a project i've created several struct in C#. The probject itself is a ASP.Net MVC 2 project. snip: struct TDummy { private char _value; public TDummy(char value) { this._value = value; // Restrictions } } I created…