Base Supplements C # Json and objects convert to and from each other

Source: Internet
Author: User

Method one. JavaScriptSerializer

Method Two. JsonConvert

Example:

WEB API Project

1. New Model:

Namespace WebApi
{
public class Product
{
public string Name {set; get;}
public string Price {set; get;}
}

public class ShoppingCart
{
public string Name {set; get;}
Public list<product> productlist {set; get;}
}
}

2. Serialization and deserialization tool classes

(1). Create a new tool class using the JavaScriptSerializer method Jsonhelper:

...

Using System.Web.Script.Serialization;

Namespace WebApi
{
public static Class Jsonhelper<t>
{
private static JavaScriptSerializer serializer = new JavaScriptSerializer ();
public static string Getjsonstr (T objectList)
{
Return serializer. Serialize (objectList);
}

public static list<t> getobjectlist<t> (String jsonstr)
{
List<t> OBJS = serializer. Deserialize<list<t>> (JSONSTR);
return OBJS;
}

public static T Getobj (String jsonstr)
{
Return serializer. Deserialize<t> (JSONSTR);
}
}
}

(1). Create a new tool class using the JsonConvert method Netjsonhelper:

...

Using Newtonsoft.json;

Namespace Webapi.common
{
public class Netjsonhelper<t>
{
public static string Getjsonstr (T obj)
{
return jsonconvert.serializeobject (obj);
}

public static T Getobj (String objjsonstr)
{
Return (T) jsonconvert.deserializeobject<t> (OBJJSONSTR);
}

public static list<t> Getobjs (String objsjsonstr)
{
Return (list<t>) jsonconvert.deserializeobject<list<t>> (OBJSJSONSTR);
}

}
}

3. New Controller Shoppingcontroller

public class Shoppingcontroller:apicontroller
{

private static ShoppingCart ShoppingCart = new ShoppingCart ();
private static string productsstr = String. Empty; Save serialized string
Public Shoppingcontroller ()
{
list<product> products = new list<product> () {
New Product () {name= "Apple", price= "$20/kg"},
New Product () {name= "Banana", price= "$15/kg"},
New Product () {name= "melon", price= "$20/kg"}
};
Shoppingcart.name = "Myfruitbaskit";
Shoppingcart.productlist = Products;
}

public string Getshoppingcart ()
{
String jsonstr = jsonhelper<shoppingcart>. Getjsonstr (ShoppingCart);
PRODUCTSSTR=JSONSTR; Saves the serialized string for deserialization of the interface test
return jsonstr;
}

Public ienumerable<product> getfruitlist ()
{
ShoppingCart shoppingcartobj = jsonhelper<shoppingcart>. Getobj (PRODUCTSSTR);
return shoppingcartobj.productlist;
}

public string Getshoppingcartbynetjson ()
{
String jsonstr = netjsonhelper<shoppingcart>. Getjsonstr (ShoppingCart);
Productsstr = Jsonstr; Saves the serialized string for deserialization of the interface test
return jsonstr;
}

Public ienumerable<product> Getfruitlistbynetjson ()
{
ShoppingCart shoppingcartobj = netjsonhelper<shoppingcart>. Getobj (PRODUCTSSTR);
return shoppingcartobj.productlist;
}

}

4. Start the Web API for testing

such as: Test JavaScriptSerializer

First: Localhost:xxxx/api/shopping/getshoppingcart

Then: localhost:xxxx/api/shopping/getfruitlist

Both methods do not have an exception when they are passed to null , regardless of whether they are serialized or deserialized

Base Supplements C # Json and objects convert to and from each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.