search

Home  >  Q&A  >  body text

java - json封装

巴扎黑巴扎黑2802 days ago2687

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:45:57

    Google’s gson is also very useful

    reply
    0
  • 阿神

    阿神2017-04-18 10:45:57

    1. Import Json processing related jar packages

    commons-beanutils-1.7.0.jar
    commons-collections-3.2.1.jar
    commons-lang-2.6.jar
    commons-logging-1.1.3.jar
    ezmorph-1.0.6.jar
    json- lib-2.4-jdk15.jar
    log4j-1.2.9.jar
    slf4j-api-1.6.4.jar

    2. Use JSONObject encapsulation

    JSONObject object = new JSONObject()
    object.put(contractCode,"2017021001")

    So the function can be simply written like this:

    public JSONObject generateJsonObject(String contractCode){
        JSONObject object = new JSONObject()
        object.put(contractCode,"2017021001")
        
        return object;
    }

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:45:57

    fastjson produced by Alibaba,
    JSONObject json = new JSONObject(); json.put("contractCode","2017021001")
    return json.toJSONString();

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:45:57

    Use gson, convenient and easy to use

    reply
    0
  • 阿神

    阿神2017-04-18 10:45:57

    If it is a maven project, you can use fastjson
    to introduce dependencies

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.17</version>
    </dependency>
    /**
     * @param contractCode
     * @return 返回封装好的 json 串
     */
    private String covert(String contractCode) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("contractCode", contractCode);
        return jsonObject.toJSONString();
    }

    reply
    0
  • 阿神

    阿神2017-04-18 10:45:57

    I have basically used some mainstream json, such as jackson, gson, fastjson, etc. Personally, I still prefer fastjson. The performance may not be as good as jackson. Spring also uses jackson internally, but the API of fastjson is very simple and clean. json, I really like it

    reply
    0
  • Cancelreply