For the direct conversion of Json and object learning, json object Conversion

Source: Internet
Author: User

For the direct conversion of Json and object learning, json object Conversion
There are many ways to learn this: the data is clearly viewed during data transmission, and the code is also clear. You can also avoid hidden bugs caused by the split function: the preparation tool is cumbersome and requires objects to be prepared (of course, each table in the project has a corresponding Model class, and a small amount of data is not recommended) ------ ask you guys, can I directly convert a Json string to an object (the object format does not need to be defined by ourselves, because the format of the object is already clear in Json)
Two methods are introduced: Method 1: The namespace for. NET Framework3.5 or a later version is using System. Runtime. Serialization. Json;

// Convert an object to a Json string public static string ObjectToJson _ (object obj) {DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj. getType (); MemoryStream stream = new MemoryStream (); serializer. writeObject (stream, obj); byte [] dataBytes = new byte [stream. length]; stream. position = 0; stream. read (dataBytes, 0, (int) stream. length); return Encoding. UTF8.GetString (dataBytes);} // convert a Json string to the public static object JsonToObject _ (string jsonString, Type type) {DataContractJsonSerializer serilizer = new DataContractJsonSerializer (type ); memoryStream stream = new MemoryStream (Encoding. UTF8.GetBytes (jsonString); return serilizer. readObject (stream );}
Type 2: Newtonsoft. Json. dll must be referenced.
// Generate a Json string public static string ObjectToJson (object obj) {return JsonConvert from an object information. serializeObject (obj);} // generate object information from a Json string public static object JsonToObject (string jsonString, Type obj) {return JsonConvert. deserializeObject (jsonString, obj );}

The following example is used:
// Convert the submitted data Json to Model string sendMessage = "[{\" Type \ ": \" exquisite stir fry \ ", \" MyLunch \": [{\ "Name \": \ "tomato scrambled eggs \", \ "Price \": \ "10 \" },{ \ "Name \": \ "oil-consuming beef \", \ "Price \": \ "14 \" },{ \ "Name \": \ "Flammulina velutipes beef \", \ "Price \": \ "16 \" },{\ "Name \": \ "shrimp scrambled eggs \", \ "Price \": \ "15 \"}]}, {\ "Type \": \ "exquisite package \", \ "MyLunch \": [{\ "Name \": \ "Business Package \", \ "Price \": \ "15 \" },{\ "Name \": \ "braised pork chops package \", \ "Price \": \ "12 \" },{ \ "Name \": \ "pretzels package \", \ "Price \": \ "10 \"}, {\ "Name \": \ "jacket steak package \", \ "Price \": \ "10 \"}] },{ \ "Type \": \ "special cover rice \", \ "MyLunch \": [{\ "Name \": \ "Back pot meat cover rice \", \ "Price \": \ "12 \" },{ \ "Name \": \ "pepper beef bread rice \", \ "Price \": \ "13 \"}, {\ "Name \": \ "rice with shredded garlic paste \", \ "Price \": \ "15 \" },{ \ "Name \": \ "Spicy chicken rice bowl \", \ "Price \": \ "12 \"}] "; List <Meal> getModel = new List <Meal> (); getModel = (List <Meal>) JsonToObject _ (sendMessage, typeof (List <Meal> )); // convert the Model to Json List <Meal> MealList = new List <Meal> (); Meal modelMeal = new Meal (); list <Lunch> myLunch = new List <Lunch> (); Lunch modelLunch = new Lunch (); modelLunch. name = "desktop halogen"; modelLunch. price = "15"; myLunch. add (modelLunch); modelMeal. myLunch = myLunch; modelMeal. type = "Friday package"; MealList. add (modelMeal); string jsonString = ObjectToJson _ (MealList); result = jsonString;

This is probably the case. Correct it by mistake! QAQ

Convert the List directly to a json object, and then how to set the value in jsp?

Java:
List list = new ArrayList (); list. add (new User ("test"); // get list
JSONObject json = JSONObject. fromObject (list); // list --> json Conversion
Sysout (json); // print [{"name": "test"}]
String content = json. toString; // toString and pass it to jsp
Response. getWriter (). write (content );
----------------------------------------------
Jsp:
Jquery code:
Function callback (json ){
Alert ("Callback:" + json. name); // print test
}

How can I convert a List to a json object and use a Json object to interact with the front-end page? Thanks!

Beanutil has a listtojson method, which is converted directly... Beanutil forgot the jar package. Check it yourself .....

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.