Java List<JSONObject>Data Conversion List<T>
Inferring from the function of the method,T
It should be a specific Java Bean type, used toList<JSONObject>
The data in theList<T>
。
The following is a detailed introduction to how to pass genericsT
And how to implement this method.
import ; import ; import ; // Define a general conversion methodpublic class GenericConverter { public static <T> List<T> convertParamsBean(Class<T> clazz, List<JSONObject> jsonObjectList) { List<T> resultList = new ArrayList<>(); for (JSONObject jsonObject : jsonObjectList) { try { // Use FastJSON to convert JSONObject to an object of the specified type T instance = (jsonObject, clazz); (instance); } catch (Exception e) { (); } } return resultList; } } // Define an example Bean classclass ExampleBean { private String name; private int age; public String getName() { return name; } public void setName(String name) { = name; } public int getAge() { return age; } public void setAge(int age) { = age; } @Override public String toString() { return "ExampleBean{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class Main { public static void main(String[] args) { List<JSONObject> jsonObjectList = new ArrayList<>(); // Create example JSONObject JSONObject jsonObject1 = new JSONObject(); ("name", "Alice"); ("age", 25); (jsonObject1); JSONObject jsonObject2 = new JSONObject(); ("name", "Bob"); ("age", 30); (jsonObject2); // Call the conversion method and pass the Class object of the ExampleBean class List<ExampleBean> exampleBeanList = (, jsonObjectList); // Print the converted result for (ExampleBean bean : exampleBeanList) { (bean); } } }
Code explanation
convertParamsBean
method:
- This method uses generics
<T>
, receive oneClass<T>
Parameters of typeclazz
And oneList<JSONObject>
Parameters of typejsonObjectList
。 - Traversal
jsonObjectList
,useMethod to each
JSONObject
Convert toclazz
The object of the type represented. - Add the converted object to
resultList
and finally return to the list.
ExampleBean
kind:
This is an example Java Bean class containingname
andage
Two attributes, and correspondinggetter
andsetter
method.
Main
Classicmain
method:
- Create a
List<JSONObject>
And add two examplesJSONObject
。 - Call
Method, delivery
As
clazz
Parameters,List<JSONObject>
Convert toList<ExampleBean>
。 - Iterate over and print the converted
List<ExampleBean>
。
Passing genericsT
Way
- Calling
convertParamsBean
When passing the specific Java Bean class when using the methodClass
Objects, e.g.。
- This way, this can be used inside the method
Class
object to create an instance of the corresponding type andJSONObject
The data in the instance is populated.
Things to note
- To ensure that the JSON processing library used (such as FastJSON) has been properly added to the project dependencies.
- The target Java Bean class must have a default parameterless constructor because
toJavaObject
Methods create objects using reflection. - if
JSONObject
The field name in the Java Bean class does not match the attribute name and may require additional configuration or processing.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.