Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Field;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Main {

    public static <T> JSONArray beansToJsonArray(T... beans)
            throws IllegalArgumentException, IllegalAccessException, JSONException {
        JSONArray jArr = new JSONArray();
        for (T bean : beans) {
            JSONObject jObj = new JSONObject();
            Field[] fields = bean.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object value = field.get(bean);

                jObj.put(name, value);
            }
            jArr.put(jObj);
        }
        return jArr;
    }
}