When should I use JSON serialization and why?

I am a little confused about when do I have to serialize objects using JSON serialization and when it is not necessary (even needless).

I've spent several days trying to send a list of simple objects using jQuery.ajax and JSON. I have a class named Product:

public class Product
{
    public string Name;
    public string Qnt;
    public string Price;
}

and a web method that returns a list filled with Products

List<Product> p = new List<Product>();

I used System.Web.Script.Serialization.JavaScriptSerializer to serialize this list in JSON and send it to the client as a string, where i format it using jQuery

$.each(msg.d, function (index, Product) {
    $('#details').append('<p>Name: ' + Product.Name 
                           + '<br />Quantity: ' + Product.Qnt 
                           + '<br />Price: ' + Product.Price + '</p>');
});

...and it wouldn't work... Because at the client side, I just get a long string (it would be parsed character by character, when I use $.each) - I learned that it should be parsed first

It took time until I realised that I just have to return it as a List of Product objects, without serialization, and client gets list of products in perfect JSON format!

EDIT : What I actually don't understand: if I serialize my list, it returns JSON formated string that have to be parsed before I can use it. If i don't serialize my list, it returns the same data, but as an object, not a string, so I can use it right away. How can I know do I have to use JSON serialization on my data, or it will always be done by framework ?


You're not returning your List<Product> directly to the client. It's not going over the wire as a List<Product> - there's no such concept at the HTTP level. However, the server-side framework you're using is performing the JSON serialization for you. If you perform serialization first, then the serialized string will then be JSON-serialized, including another level of escaping etc.


I think you need to first call $.parseJSON(msg.d) to turn the JSON string into JS Objects, that jQuery will have no problems ot iterate over. You will probably end up with an object like "yourServiceMethodCallNameResult" and that object will have an array of objects (your products).


You havent mentioned how you query for the product list. The problem with JavaScriptSerializer is that it returns a JSON output wrapped in a XML tag, which makes it a XMLObject on the client. You can simply get the inner text content and use $.parseJSON() method to convert it into a JSON object on the client.

An example which queries a WCF service which uses JavaScriptSerializer to serialize a Product list into JSON.

server side:

JavaScriptSerializer js = new JavaScriptSerializer();
List<Product> p = new List<Product>()
{
new Product() { Name = "one", Price = "1", Qnt = "1" },
new Product() { Name = "two", Price = "1", Qnt = "1" },
new Product() { Name = "three", Price = "1", Qnt = "1" },
new Product() { Name = "four", Price = "1", Qnt = "1" },
new Product() { Name = "five", Price = "1", Qnt = "1" },
new Product() { Name = "six", Price = "1", Qnt = "1" }
};
return js.Serialize(p);

client side:

$.get("http://server/service/GetProducts", function (data) {
    var jsonObj = $.parseJSON(data.firstChild.textContent);
    var obj1 = jsonObj[0];
});
链接地址: http://www.djcxy.com/p/47838.html

上一篇: 什么是在CSV文件中存储和检索布尔值的便捷方式

下一篇: 我应该何时使用JSON序列化,为什么?