The conversion of JSON to Java entity classes __js

Source: Internet
Author: User
Tags static class

JSON (JavaScript Object notation) is a lightweight data interchange format. Commonly used with the foreground interaction, in the use of Java and JSON will sometimes encounter the conversion between the situation, as follows:

Note To import the related JSON package before using

Define entity classes:

/**
* Inner class
* @author Administrator
*
*/
public static Class test{
private String name;
Private String word;
private String color;
Private String image;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getword () {
return word;
}
public void Setword (String word) {
This.word = Word;
}
Public String GetColor () {
return color;
}
public void SetColor (String color) {
This.color = color;
}
Public String GetImage () {
return image;
}
public void SetImage (String image) {
This.image = image;
}
}

/**
* JSON turn Java class
* @param String Json
* @throws IOException
* @throws jsonmappingexception
* @throws jsonparseexception
*/
Public Test Jsontoclass (String json) throws Jsonparseexception, Jsonmappingexception, ioexception{
Jsonobject Jsonobj=jsonobject.fromobject (JSON);
SYSTEM.OUT.PRINTLN ("JSON gets object:" +jsonobj.get ("name"));

Both of these methods are available

Test test= (Test) Jsonobject.tobean (Jsonobj, Test.class);
Test Test=mapper.readvalue (JSON, test.class);
return test;
}

/**
* Java class turn JSON
* @param class
*/
Public String Classtojson (Object test) {

Both of these methods are available
return Jsonserializer.tojson (Test). ToString ();
return Jsonobject.fromobject (Test). ToString ();
}

The conversion of list and JSON can refer to the above methods, divide the list into entities for conversion, and refer to the related integrated class packages.

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.