SoFunction
Updated on 2025-05-20

How to convert data in Java List to List

Java List<JSONObject>Data Conversion List<T>

Inferring from the function of the method,TIt 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 genericsTAnd how to implement this method.

import ;
import ;
import ;

// Define a general conversion methodpublic class GenericConverter {
    public static &lt;T&gt; List&lt;T&gt; convertParamsBean(Class&lt;T&gt; clazz, List&lt;JSONObject&gt; jsonObjectList) {
        List&lt;T&gt; resultList = new ArrayList&lt;&gt;();
        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&lt;JSONObject&gt; jsonObjectList = new ArrayList&lt;&gt;();

        // 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&lt;ExampleBean&gt; 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 typeclazzAnd oneList<JSONObject>Parameters of typejsonObjectList
  • TraversaljsonObjectList,useMethod to eachJSONObjectConvert toclazzThe object of the type represented.
  • Add the converted object toresultListand finally return to the list.

ExampleBean kind

This is an example Java Bean class containingnameandageTwo attributes, and correspondinggetterandsettermethod.

Main Classicmain method

  • Create aList<JSONObject>And add two examplesJSONObject
  • CallMethod, deliveryAsclazzParameters,List<JSONObject>Convert toList<ExampleBean>
  • Iterate over and print the convertedList<ExampleBean>

Passing genericsTWay

  • CallingconvertParamsBeanWhen passing the specific Java Bean class when using the methodClassObjects, e.g.
  • This way, this can be used inside the methodClassobject to create an instance of the corresponding type andJSONObjectThe 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 becausetoJavaObjectMethods create objects using reflection.
  • ifJSONObjectThe 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.