Android data processing: Using annotation to parse JSON strings into java objects

Source: Internet
Author: User

Annotation is a very useful mechanism provided by the Java language. It can greatly simplify the amount of code when it is used to describe Java objects. Before reading this article, you 'd better first understand the basic knowledge of Java Annotation. In this aspect, you can easily find learning materials on the Internet, such as the following: java annotation: the formal conversion between JSON and Java objects is provided in some J2EE frameworks, but not implemented in Android. This document describes how to use annotation to implement a formal method to convert JSON strings and Java objects on Android. First, define an annotaion type. when defining a Java class, you can use this type to mark which fields need to be output to a JSON string. [Java] @ Target ({ElementType. FIELD}) @ Retention (RetentionPolicy. RUNTIME) public @ interface JSONValue {String tag () default "";} This annotation includes a variable tag, which refers to the leading String (name) of the marked domain in the JSON String ). Define a conversion tool class for conversion between Java objects and JSON objects. [Java] public class JSONConverter {public static void fromJSon (String json_string, Object o) throws Exception {JSONObject = new JSONObject (json_string); JSONObject jo = new JSONObject (); field [] fields = o. getClass (). getFields (); for (Field f: fields) {if (f. isAnnotationPresent (QueryValue. class) {JSONValue jv = f. getAnnotation (JSONValue. class); String tag = jv. tag (); if (tag. length ()> 0) {if (f. GetType (). getSimpleName (). equals ("int") {f. setInt (o, jo. optInt (tag);} else {f. set (o, jo. optString (tag) ;}}} return jo. toString ();} public static String toJSon (Object o) throws Exception {JSONObject = new JSONObject (); Field [] fields = o. getClass (). getFields (); for (Field f: fields) {if (f. isAnnotationPresent (QueryValue. class) {JSONValue jv = f. getAnnotation (JSONValue. class); String Tag = jv. tag (); if (tag. length ()> 0) {if (f. getType (). getSimpleName (). equals ("int") {jo. put (tag, f. getInt (o);} else {Object v = f. get (o); if (v! = Null) jo. put (tag, v) ;}}} return jo. toString () ;}} the preceding tool class implements two static methods: fromJSON () is used to parse a Java object from a JSON object, and toJSON () is used to convert a Java object to a JSON object. In the preceding example, only int and String data types are supported, which can meet the needs of most applications. If you want to support other types, you need to modify them according to the example. In addition, the preceding example does not support the JSON array type. Then, define the Java type that needs to be converted to a JSON object, and use the annotation defined earlier, for example: [java] class MyObject {@ JSONValue (tag = "id ") public int mId; @ JSONValue (tag = "name") public String mName;} when defining the domain to be output to the JSON object, define the domain as public, use JSONValue to mark and specify the leading string (name) of the field in the JSON object ). During use, you can easily convert this type of object into a JSON string: [java] MyObject o = new MyObject (); o. mId = 123; o. mName = "zhangsan"; String json = JSONConverter. toJSON (o); the converted JSON string type is {"id": 123, "name": "Zhang San"}; JSON strings can also be easily parsed into Java objects: [java] String json_string = {"id": 123, "name": "Zhang San"}; MyObject o = new MyObject (); JSONConverter. fromJSON (json_string, o );

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.