Conversion between Java class objects and JSON data

Source: Internet
Author: User

There are too many table fields and you don't want to save them separately to the data, so use a low frequency, so consider using the Jave class--JSON data conversion.

1. Class conversion JSON, and save to data

Attribute.java

1 /**2 * Attribute data structure3  *4  */5  Public classAttribute {6 7     Private Static Final LongSerialversionuid = -1l;8 9     PrivateLong Attrid;Ten  One     PrivateList<attributevalue>attributevalues; A  -     PrivateLong catid; -  the     PrivateString Cnname; -  -     PrivateBoolean customizeimage; -  +     PrivateBoolean Customizevalue; -  +     PrivateString Enname; A  at     PrivateString InputType; -  -     PrivateBoolean keyattr; -  -     PrivateBoolean Locator; -  in     PrivateLong order; -  to     PrivateLong Parentvalue; +  -     PrivateBoolean required; the  *     PrivateList<string> units;
Attribute.java

Attributeservice.java

1 // convert to JSON string 2 list<attribute> attributes = response.getattributes (); 3 Jsonarray jsonarray = jsonarray.fromobject (attributes); 4 // Save to data 5 this. Save (Jsonarray.tostring ());

2. JSON conversion into class

Attributeservice.java

1 //get JSON properties from the database2String Jsonattribute = This. getattribute ();3 //convert to JSON array4Jsonarray jsonattr =Jsonarray.fromobject (jsonattribute);5 //because the attribute class contains List<attributevalue>, you need to add a subset6map<string, class> classmap =NewHashmap<string, class>(); 7Classmap.put ("Attributevalues", AttributeValue.class); 8 //Final Convert List object complete9List<attribute> listproductattributes = (list<attribute>) jsonarray.tolist (jsonAttr, Attribute.class, Classmap);

Another way to deal with the data, this will be more simple, the operation of the database more convenient, but in the database is not easy to read, page-side display and sub-table sub-field storage no difference.

Conversion between Java class objects and JSON data

Related Article

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.