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 OverflowCheckout 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
Yes.
If the JSON object is available, you can use :
var aString = JSON.stringify(anObject);
to transform an object into JSON string.
You can also convert a string into an object with
var obj = JSON.parse(aString)
To be sure that JSON is available, you can include this file https://github.com/douglascrockford/JSON-js
ยป npm install json-serializer
ยป npm install javascript-serializer
JSON-js - JSON in JavaScript.
To convert an object to a string, use JSON.stringify:
var json_text = JSON.stringify(your_object, null, 2);
To convert a JSON string to object, use JSON.parse:
var your_object = JSON.parse(json_text);
It was recently recommended by John Resig:
...PLEASE start migrating your JSON-using applications over to Crockford's json2.js. It is fully compatible with the ECMAScript 5 specification and gracefully degrades if a native (faster!) implementation exists.
In fact, I just landed a change in jQuery yesterday that utilizes the JSON.parse method if it exists, now that it has been completely specified.
I tend to trust what he says on JavaScript matters :)
All modern browsers (and many older ones which aren't ancient) support the JSON object natively. The current version of Crockford's JSON library will only define JSON.stringify and JSON.parse if they're not already defined, leaving any browser native implementation intact.
I've been using jquery-json for 6 months and it works great. It's very simple to use:
var myObj = {foo: "bar", "baz": "wockaflockafliz"};
$.toJSON(myObj);
// Result: {"foo":"bar","baz":"wockaflockafliz"}
- You need to create a class that holds the user values, just like the response class
User. 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.
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.
ยป npm install serialize-javascript