Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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封送/反封送到Json?_Java_Json - Fatal编程技术网

如何将Java封送/反封送到Json?

如何将Java封送/反封送到Json?,java,json,Java,Json,我想将JavaPOJO类转换为JSON。但是,我需要更改JSON中的键名。例如: class Employee { private int empId; private String empName; } Json应该是:{EMP_ID:101,EMP_NAME:tesst} 我发现Gson和其他库可以这样做,但如何更改JSON键名,如map empId=>EMP_ID?您可以在Gson中使用@SerializedName注释: class Employee { @Se

我想将JavaPOJO类转换为JSON。但是,我需要更改JSON中的键名。例如:

class Employee {
    private int empId;
    private String empName;
}
Json应该是:{EMP_ID:101,EMP_NAME:tesst}


我发现Gson和其他库可以这样做,但如何更改JSON键名,如map empId=>EMP_ID?

您可以在Gson中使用@SerializedName注释:

class Employee {
    @SerializedName("EMP_ID")
    private int empId;
    @SerializedName("EMP_NAME")
    private String empName;
}

您可以使用反射,但键将保持与变量名相同。 我正在对bean类做同样的操作,以便从它们生成json

希望这会有帮助

public static String getRequestJsonString(Object request,boolean withNullValue) {

    JSONObject jObject = new JSONObject();

    try {
        if (request != null) {
            for (Map.Entry<String, String> row : mapProperties(request,withNullValue).entrySet()) {

                jObject.put(row.getKey(), row.getValue());
            }
        }

        Log.v(TAG, jObject.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

   return jObject.toString();
}


public static Map<String, String> mapProperties(Object bean,boolean withNullValue) throws Exception {
    Map<String, String> properties = new HashMap<>();
    try {
        for (Method method : bean.getClass().getDeclaredMethods()) {
            if (Modifier.isPublic(method.getModifiers())
                    && method.getParameterTypes().length == 0
                    && method.getReturnType() != void.class
                    && method.getName().matches("^(get|is).+")
                    ) {
                String name = method.getName().replaceAll("^(get|is)", "");
                name = Character.toLowerCase(name.charAt(0)) + (name.length() > 1 ? name.substring(1) : "");

                Object objValue = method.invoke(bean);

                if (objValue != null) {
                    String value = String.valueOf(objValue);
                    //String value = method.invoke(bean).toString();
                    properties.put(name, value);
                } else {

                    if (withNullValue)
                    {
                        properties.put(name, "");
                    }
                }

            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return properties;
}