通用JSON数据生成器2

博主针对之前创建的JSON生成器进行了改进,通过引入注解方式解决之前命名限制问题,允许自定义JSON字段名,适应不同编程风格的需求。同时,通过注解控制哪些字段需要在输出时包含,解决了不想输出某些域的问题,提高了代码的灵活性。
摘要由CSDN通过智能技术生成

前几天写了一个通用JSON生成器,但是用着用着觉得意犹未尽,不是特别顺手。原因有二:

  • 上个方法要求filed的域就是json数据中的域名称,这样很受限,特别是在发送给前端、或者JNI时,可能C语言的风格是使用下划线,而不是驼峰法,比如userName是java风格,而user_name则C语言常常用,那么,如果要让我把filed名字都变成user_name这种形式,我会受不了的。
  • 对于有些域而言,可能我并不想输出,比如我仅仅利用该域做运算,而不想发送给前端,那么前者就得要求重写一个类了。这样也很不友好。

因此,我利用注解的方法重写了上个类。1、创建Output注解;2、然后在输出类中添加Output注解;3、然后在注解中添加要返回的域的名字,对于不想输出的域,不添加注解即可。

Output.java[注解类]

package com.dacas.json.entity;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by dave on 2016/3/12.
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Output {
    public String value();//Output中使用value作为注解域,使用value可以作为“快捷方式”
}

GenerateJSON.java
package com.dacas.json.entity;

import org.json.JSONArray;
import org.json.JSONObject;
import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by dave on 2016/3/6.
 */
public class GenerateJSON {
    public static <T> JSONObject getJSON(T t,Class<T> tClass) throws Exception{
        Field[] fileds = tClass.getFields();
        JSONObject jsonObject = new JSONObject();
        for (Field field:fileds){
            Output output = field.getAnnotation(Output.class);
            if(output != null){
                String name = output.value();
                Object value = field.get(t);
                jsonObject.put(name,value);
            }
        }
        return jsonObject;
    }
    public static <T> JSONArray getJSON(List<T> tlist,Class<T> tClass) throws Exception{
        JSONArray jsonArray = new JSONArray();
        for(T tmpT:tlist){
            jsonArray.put(getJSON(tmpT,tClass));
        }
        return jsonArray;
    }
    public static <T> String getJSONStr(T t,Class<T> tClass) throws Exception{
        JSONObject object = getJSON(t,tClass);
        return object.toString();
    }
    public static <T> String getJSONStr(List<T> tList,Class<T> tClass) throws Exception{
        JSONArray array = getJSON(tList,tClass);
        return array.toString();
    }
}

Test.java[测试java实体类]
package com.dacas.json.entity;

/**
 * Created by dave on 2016/3/6.
 */
public class Test {
    @Output("_id")
    public int id;
    public String value;

    public Test(int id,String value){
        this.id = id;
        this.value = value;
    }
}

【main类】
import com.dacas.json.entity.GenerateJSON;
import com.dacas.json.entity.Test;

import java.util.LinkedList;

/**
 * Created by dave on 2016/3/6.
 */
public class TestString {
    public static void main(String[] args){
        try {
            LinkedList<Test> testList = new LinkedList<>();
            for(int i = 0;i<10;i++){
                Test test = new Test(i,i+"");
                testList.add(test);
            }
            String result = GenerateJSON.getJSONStr(testList,Test.class);
            System.out.println(result);
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值