简体   繁体   English

Java: JSONObject.toString() - 在冒号后添加额外的空格 (":" -> ": ")

[英]Java: JSONObject.toString() - adding extra space after colon (":" -> ": ")

Been googling a while but haven't found anything useful since keywords are too common.谷歌搜索了一段时间,但没有发现任何有用的东西,因为关键字太常见了。

Currently in my Java code method toString() (called on some JSONObject object) produces output like this:目前在我的 Java 代码方法toString() (在一些JSONObject对象上调用)产生如下输出:

{"key_name":<numerical_value>}

while what I need (due to the fact that parser on the other side of the project is imperfect) is:而我需要的(由于项目另一端的解析器不完美)是:

{"key_name": <numerical_value>}

The only difference is that extra space after colon.唯一的区别是冒号后的额外空间。 Is there any JSONObject builtin way to do it or do I need some handwritten simple string operation for it?是否有任何JSONObject内置方法可以做到这一点,或者我是否需要一些手写的简单字符串操作?

I can only imagine building a custom JSONStringer .我只能想象构建一个自定义的JSONStringer In fact, it could be a JSONWriter built using a StringWriter .事实上,它可能是一个使用StringWriter构建的JSONWriter Then you override all the value methods to add a space before calling parent class method.然后在调用父类方法之前覆盖所有value方法以添加空格。

class JSONSpaceStringer extends JSONWriter {
    private StringWriter sw;

    JSONSpaceStringer(StringWriter sw) {
        parent(sw); // initialize parent
        this.sw = sw;
    }

    @Override
    public JSONWriter value(long l) throws JSONException {
        sw.write(" "); // add an initial space
        parent.value(l); // and let parent class do the job...
    }

// same for all other value methods
//...
}

use the below code.使用下面的代码。 it will help you to solve the problem.它将帮助您解决问题。

 package com.javamad.utils;

    import java.io.IOException;

    import org.apache.log4j.Logger;
    import org.codehaus.jackson.JsonGenerationException;
    import org.codehaus.jackson.JsonParseException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;

    public class JsonUtils {

        private static Logger logger = Logger.getLogger(JsonUtils.class.getName());


        public static <T> T jsonToJavaObject(String jsonRequest, Class<T> valueType)
                throws JsonParseException, JsonMappingException, IOException {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE,false);     
            T finalJavaRequest = objectMapper.readValue(jsonRequest, valueType);
            return finalJavaRequest;

        }

        public static String javaToJson(Object o) {
            String jsonString = null;
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE,true);  
                jsonString = objectMapper.writeValueAsString(o);

            } catch (JsonGenerationException e) {
                logger.error(e);
            } catch (JsonMappingException e) {
                logger.error(e);
            } catch (IOException e) {
                logger.error(e);
            }
            return jsonString;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM