Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将java列表对象的特定字段转换为jsonarray_Java_Json - Fatal编程技术网

如何将java列表对象的特定字段转换为jsonarray

如何将java列表对象的特定字段转换为jsonarray,java,json,Java,Json,我有一个班:人 class Person{ String name; String age; } 我想将人员列表转换为jsonArray,但只转换结果中的name字段。然而,如果我使用 List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Jack","12")); JSONArray result = JSONArray.fromObject(persons);

我有一个班:人

class Person{
    String name;
    String age;
}
我想将人员列表转换为jsonArray,但只转换结果中的name字段。然而,如果我使用

List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Jack","12"));
JSONArray result = JSONArray.fromObject(persons);
List persons=new ArrayList();
新增(新增人员(“杰克”、“12”));
JSONArray result=JSONArray.fromObject(个人);
结果将包括年龄字段


我能做什么?

我的解决方案是使用util函数创建JSONObject,使用:

JSONArray result = createJsonObjects(persons, "name", "name")

import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

public static JSONArray createJsonObjects( List< ? > objs, String propertyNames, String jsonKeys )
{
    Assert.hasText( propertyNames );
    Assert.notNull( objs );

    JSONArray result = new JSONArray();
    String[] propertyNameArray = propertyNames.split( ";" );
    String[] jsonKeysArray = propertyNameArray;
    if ( StringUtils.hasText( jsonKeys ) )
    {
        jsonKeysArray = jsonKeys.split( ";" );
    }

    Assert.isTrue( jsonKeysArray.length == propertyNameArray.length );
    try
    {
        Method[] methods = new Method[ propertyNameArray.length ];
        for ( Object obj : objs )
        {
            for ( int i = 0; i < propertyNameArray.length; i++ )
            {
                methods[ i ] = BeanUtils.getPropertyDescriptor( obj.getClass(),
                                                                propertyNameArray[ i ] ).getReadMethod();
            }
            JSONObject json = new JSONObject();
            for ( int i = 0; i < propertyNameArray.length; i++ )
            {
                if ( obj != null )
                    json.element( jsonKeysArray[ i ],
                                  ReflectionUtils.invokeMethod( methods[ i ], obj ) );
            }
            result.add( json );
        }

    }
    catch ( Exception e )
    {
        throw new RuntimeException( e );
    }

    return result;
}
JSONArray result=createJsonObjects(persons,“name”,“name”)
导入org.springframework.beans.BeanUtils;
导入org.springframework.util.Assert;
导入org.springframework.util.ReflectionUtils;
导入org.springframework.util.StringUtils;
公共静态JSONArray创建JSONObject(列表<?>对象、字符串属性名称、字符串jsonKeys)
{
Assert.hasText(propertyNames);
Assert.notNull(objs);
JSONArray结果=新的JSONArray();
字符串[]propertyNameArray=propertyNames.split(“;”);
字符串[]jsonKeysArray=propertyNameArray;
if(StringUtils.hasText(jsonKeys))
{
jsonKeysArray=jsonKeys.split(“;”);
}
isTrue(jsonKeysArray.length==propertyNameArray.length);
尝试
{
方法[]方法=新方法[propertyNameArray.length];
用于(对象对象对象:对象对象对象)
{
对于(int i=0;i
如何转换仅包含年龄的人