Checkout JSON.stringify() and JSON.parse() in JSON2 documentation

Example:

myData = JSON.parse(text); // from json string to js object

var myJSONText = JSON.stringify(myObject, replacer); // js object to json string
Answer from techfoobar on Stack Overflow
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ json-serializer
json-serializer - npm
JSON compatible serializer and deserializer for javascript.. Latest version: 2.0.12, last published: 7 years ago. Start using json-serializer in your project by running `npm i json-serializer`. There are 1 other projects in the npm registry using json-serializer.
      ยป npm install json-serializer
    
Published: Nov 29, 2018
Version: 2.0.12
Author: Peter Fronc
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ javascript-serializer
javascript-serializer - npm
September 22, 2018 - A powerful object to json serializer/deserializer. In addition to the standard json data types (Number, String, Boolean, Array, Object & Null), this module helps you also to serialize & deserialize more advanced data types such as: Date, RegExp ...
      ยป npm install javascript-serializer
    
Published: Sep 22, 2018
Version: 1.0.0
Author: Shahar Talmi
๐ŸŒ
GitHub
github.com โ€บ brillout โ€บ json-serializer
GitHub - brillout/json-serializer: Same as JSON but with added support for Date, undefined, NaN, Infinity, RegExp and more. ยท GitHub
JSON is a good serializer for JavaScript values but is lacking some JavaScript types such as Date: import assert from 'assert' let obj = { time: new Date('2042-01-01'), } // JSON converts dates to strings assert(obj.time.constructor === Date) ...
Starred by 52 users
Forked by 9 users
Languages: TypeScript 69.2% | JavaScript 30.8%
๐ŸŒ
GitHub
gist.github.com โ€บ 652028
JSON Serializer with custom Javascript converter (used to Datetime formatting) ยท GitHub
JSON Serializer with custom Javascript converter (used to Datetime formatting) - JSONSerializer.aspx.cs
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.web.script.serialization.javascriptserializer.serialize
JavaScriptSerializer.Serialize Method (System.Web.Script.Serialization) | Microsoft Learn
When the JavaScriptSerializer instance is serializing a type for which a custom converter is registered, the serializer calls the Serialize method to obtain the dictionary of name/value pairs that will be converted to a JSON string.
Find elsewhere
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.web.script.serialization.javascriptserializer
JavaScriptSerializer Class (System.Web.Script.Serialization) | Microsoft Learn
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 ...
Top answer
1 of 6
39
  1. You need to create a class that holds the user values, just like the response class User.
  2. Add a property to the Response class 'user' with the type of the new class for the user values User.

    public class Response {
    
        public string id { get; set; }
        public string text { get; set; }
        public string url { get; set; }
        public string width { get; set; }
        public string height { get; set; }
        public string size { get; set; }
        public string type { get; set; }
        public string timestamp { get; set; }
        public User user { get; set; }
    
    }
    
    public class User {
    
        public int id { get; set; }
        public string screen_name { get; set; }
    
    }
    

In general you should make sure the property types of the json and your CLR classes match up. It seems that the structure that you're trying to deserialize contains multiple number values (most likely int). I'm not sure if the JavaScriptSerializer is able to deserialize numbers into string fields automatically, but you should try to match your CLR type as close to the actual data as possible anyway.

2 of 6
22

Assuming you don't want to create another class, you can always let the deserializer give you a dictionary of key-value-pairs, like so:

string s = //{ "user" : {    "id" : 12345,    "screen_name" : "twitpicuser"}};
var serializer = new JavaScriptSerializer();
var result = serializer.DeserializeObject(s);

You'll get back something, where you can do:

var userId = int.Parse(result["user"]["id"]); // or (int)result["user"]["id"] depending on how the JSON is serialized.
// etc.

Look at result in the debugger to see, what's in there.

๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ cross-browser json serialization in javascript
Cross-browser JSON Serialization in JavaScript โ€” SitePoint
February 13, 2024 - JSON serialization in JavaScript is the process of converting JavaScript objects into a JSON string format. This is done using the JSON.stringify() method. The resulting JSON string can be stored or sent to a server.
๐ŸŒ
Tales from the Evil Empire
weblogs.asp.net โ€บ hajan โ€บ javascriptserializer-dictionary-to-json-serialization-and-deserialization
Hajan Selmani - JavaScriptSerializer โ€“ Dictionary to JSON Serialization and Deserialization
In this blog post, I will show on the simplest way how you can use this JavaScriptSerializer to serialize Dictionary collection to JSON string and deserialize it back to Dictionary.
๐ŸŒ
Wordpress
wingkaiwan.wordpress.com โ€บ 2012 โ€บ 12 โ€บ 28 โ€บ replacing-mvc-javascriptserializer-with-json-net-jsonserializer
Replacing MVC JavascriptSerializer with JSON.NET JsonSerializer | Ricky Wan's Technical Blog
December 28, 2012 - It is used when we return JsonResult by calling Controller#Json() or simply instantiate a JsonResult directly. It is also used during model binding by the JsonValueProviderFactory for deserializing request body to action parameter.
๐ŸŒ
Tomas Vera Online
tomasvera.com โ€บ home โ€บ programming โ€บ javascriptserializer example โ€“ parsing json with c#
JavascriptSerializer Example - Parsing JSON with C# - Tomas Vera Online
May 24, 2021 - You create an instance of the JavaScriptSerializer class and then call itโ€™s Deserialize function to operate on the JSON string. You will get back a dictionary object of type Dictionary<string, object>. This dictionary can then be examined and referenced as you would any other dictionary object.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ serialize-javascript
serialize-javascript - npm
August 29, 2026 - Serialize JavaScript to a superset of JSON that includes regular expressions, dates and functions.
      ยป npm install serialize-javascript
    
Published: Sep 23, 2026
Version: 7.1.2
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ implementing-json-serialization-in-js
Your Guide to Implementing JSON Serialization in JavaScript
JSON.stringify() is a powerful method in JavaScript that allows developers to convert JavaScript objects into JSON strings, which can then be transmitted over a network or stored in a file.
๐ŸŒ
The Burning Monk
theburningmonk.com โ€บ home โ€บ json serialization โ€“ datacontractjsonserializer vs javascriptserializer
JSON serialization โ€“ DataContractJsonSerializer vs JavaScriptSerializer | theburningmonk.com
April 20, 2015 - Without doubt the biggest difference between the two is that DataContractJsonSerializer only works with types decorated with the DataContract attribute, but JavaScriptSerializer can work with any object.