1. The JSON string is converted to Java Object 1.1. JSON string converted to Java bean
Json2bean.java
Package Jackson;import java.io.IOException;import Org.codehaus.jackson.map.ObjectMapper;Public class Json2bean {Public static void main (st
; Computer comp; //One computer per person, one class can be used as a property Public voidStudy () {SYSTEM.OUT.PRINTLN ("Our students learn all with the" +comp.brand+ "CPU of the computer is" +comp.cpu); } Public voidSayHello (String sname) {System.out.println (name+ "Say hello to" +sname+ "); } Public Static voidMain (string[] args) {computer C=Newcomputer (); C.brand= "Lenovo"; C.cpu= 920; Student S1=NewStudent (); //student has computer this propertyS1.comp =C;
In java, xml is converted to a json object, xmljson
1. A jar package is required:Json-lib-2.4-jdk15.jar and xom-1.2.5.jar, maven Repository: Net. sf. json-libJson-lib2.4Jdk15 XomXom1.2.5 2. Code:Public static JSON xmlToJson (String xml ){XMLSerializer xmlSerializer = new
has been working on the interaction of Spring 3.1.1 MVC + dojo1.8 to map the page's JSON data directly to Java objects through spring MVC. It's consistent with the following anomaly.
Java.io.EOFException:No content to map to Object due to end of input at Org.codehaus.jackson.map.objectmapper._initforre
Ading (objectmapper.java:2775) at Org.codehaus.jackson.map.o
Default format
Copy Code code as follows:
public class MyClass implements serializable{
...}
Serialization:
Copy Code code as follows:
ObjectOutputStream output = new ObjectOutputStream (new FileOutputStream (OutputPath));
Output.writeobject (MyObject);
Deserialization:
Copy Code code as follows:
ObjectInputStream input = new ObjectInputStream (new FileInputStream (InputPath));
Return (MyClass) input.readobject ();
The front-end commits JSON string format data, and the Java backend receives the JSON string data through a custom method and translates it into a Jsonobject object, as the code is relegated to the Requestdata.java class:public static Jsonobject Getrequestjsonobj (HttpServletRequest request) {InputStreamReader reader =
Jsonobject jsonobject=jsonobject.fromobject (map) do not have any response to this time and the solutionhttp://blog.csdn.net/tjcyjd/article/details/7007487Jsonobject,jsonarray User ManualHttp://www.open-open.com/lib/view/open1391842371442.htmlJsonobject.fromobject (map) (conversion of JSON to Java data)http://blog.csdn.net/sivyer123/article/details/18255689When using JS
In fact, there has been done to the Java object to the JSON array, JSON object, but at that time already provided the jar package, and today need to turn Java to JSON, but no jar bag, h
ImportCom.alibaba.fastjson.JSONObject;Importcom.google.common.base.Preconditions;Importcom.google.common.base.Strings;Importjava.util.ArrayList;Importjava.util.List;/*** Created by Mi on 16-12-22.*/ Public classSerializationdefine { Public StaticString segsymbols = "# #segSymbols # #";//Split Symbol Public StaticString object2string (Object obj) {preconditions.checkargument (obj!=NULL, "serialized object
How data in the foreground JSON format is passed into the background1. Assemble the data that will be in the background into a JSON-formatted string:var jsonstr = [{' Name ': ' Jim ', ' Age ': $}, {' name ': ' King ', ' age ': 26},{' name ': ' Jge ', ' Age ': 30}]2. Ajax request backstage using jqueryJquery.ajax ({Type: "Post",Url:url,DataType: ' JSON ',Data: {'
Java converts a JSON string to an entity class object @SuppressWarnings ("unchecked") public static Java converts a JSON string into an entity class object, based on the Net.sf.json implementation
The development process encountered some object to string and string to the object of the problem, wasted a long time, now with the proficiency of some, summarized as follows:1. The string should be defined as JSON resolvable, such as {"Name": "A", "param": "B"}, not {"A": "B"}2. Use the Open source project: FastjsonNeed to introduce:Import Com.alibaba.fastjson.J
//Change the last ', ' to '} ': theSb.setcharat (Sb.length ()-1, '} '); - returnsb.tostring (); the } the the Public StaticString ToJson (Object o) {94 if(O = =NULL) the return"NULL"; the if(OinstanceofString) the returnString2json (String) o);98 if(OinstanceofBoolean) About returnBoolean2json ((Boolean) o); - if(OinstanceofNumber )101 returnNumb
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.