java - json封装
巴扎黑
巴扎黑 2017-04-18 10:43:57
0
6
2662
巴扎黑
巴扎黑

reply all(6)
大家讲道理

Google’s gson is also very useful

阿神

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;
}
黄舟

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

左手右手慢动作

Use gson, convenient and easy to use

阿神

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();
}
阿神

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!