将java的list通过gson转化为json示例

用jmeter做接口测试,公司的后台是php,接口参数通常都是:

filters:[{"field":"user_id","field_type":7,"operator":"is_null","value":null,"filter_no":1},{"field":"pool_id","field_type":"3","operator":"=","value":0,"filter_no":2}]

handlers:

[{"type":"field","config":[{"field":"group_id","field_type":"3","value":10871894890},{"field":"star","field_type":"5","value":0}]},{"type":"assign","config":[{"field":"user_id","field_type":7,"value":{"user_num":[],"last_owner":"","user_id":"38986593","last_owner_ids":""},"method":"share"}]},{"type":"notify","config":{"addition_user":["38986593","55227644"],"user_field":["user_id","last_owner"],"subject":"test123","content":"

test{ {name}}

","channel":{"email":0,"message":1,"app":1,"desktop":1},"contentText":"

test#客户_公司名称#

","subjectText":"test123"}}]

这种格式的,在jmeter中如果生成这种格式的参数呢,只能通过java来实现,然后打成jar包,在jmeter中调用

如果将java的list通过gson转化为json,下面就将上面的参数来示例一次

Filters类:

package com.crm.workflow;

public class Fitles {

/**

* "field": "biz_type",

"field_type": "3",

"operator": "=",

"value": "贸易商",

"filter_no": 1

*/

  private String field;

  private String field_type;

  private String operator;

  private String value;

      private String filter_no;

  @Override

public String toString() {

return "Fitles [field=" + field + ", field_type=" + field_type + ", operator=" + operator + ", value=" + value

+ ", filter_no=" + filter_no + "]";

}  

  public String getField() {

return field;

}

public void setField(String field) {

this.field = field;

}

public String getField_type() {

return field_type;

}

public void setField_type(String field_type) {

this.field_type = field_type;

}

public String getOperator() {

return operator;

}

public void setOperator(String operator) {

this.operator = operator;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

public String getFilter_no() {

return filter_no;

}

public void setFilter_no(String filter_no) {

this.filter_no = filter_no;

}

}

config类:

package com.crm.workflow;

 

public class Config {

/**

* "filed":"star"

* "field_type":"5"

* "value":"1"

*/

private String filed;

private String field_type;

private String value;

 

@Override

public String toString() {

return "Config [filed=" + filed + ", field_type=" + field_type + ", value=" + value + "]";

}

public String getFiled() {

return filed;

}

public void setFiled(String filed) {

this.filed = filed;

}

public String getField_type() {

return field_type;

}

public void setField_type(String field_type) {

this.field_type = field_type;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

}

Handlers类:

package com.crm.workflow;

 

import java.util.List;

 

public class Handlers {

/**

*/

 

private String type;

private List config;

@Override

public String toString() {

return "Handlers [type=" + type + ", config=" + config + "]";

}

    public void setType(String type) {

         this.type = type;

     }

     public String getType() {

         return type;

     }

 

    public void setConfig(List config) {

         this.config = config;

     }

     public List getConfig() {

         return config;

     }

}

Main函数:

package com.crm.workflow;

import java.util.ArrayList;

import java.util.List;

import com.google.gson.Gson;

public class Main {

public static void main(String[] args) {

Gson gson = new Gson();

Fitles fitles = new Fitles();

fitles.setField("star");

fitles.setField_type("5");

fitles.setFilter_no("1");

fitles.setOperator("=");

Fitles fitles1 = new Fitles();

fitles1.setValue("5星");

fitles1.setField("big_zie");

fitles1.setField_type("5");

fitles1.setFilter_no("1");

fitles1.setOperator("=");

fitles1.setValue("贸易商");

List list = new ArrayList();

list.add(fitles);

list.add(fitles1);

Config config1 = new Config();

config1.setField_type("3");

config1.setFiled("测试");

config1.setValue("hello");

 List config = new ArrayList();

 config.add(config1);

Handlers hand = new Handlers();

hand.setConfig(config);

hand.setType("files");

String handler = gson.toJson(hand);

System.out.println(handler);

String gs = gson.toJson(list);

System.out.print(gs);

}

}

运行的结果:

handlers:{"type":"files","config":[{"filed":"测试","field_type":"3","value":"hello"}]}

gs:[{"field":"star","field_type":"5","operator":"\u003d","filter_no":"1"},{"field":"big_zie","field_type":"5","operator":"\u003d","value":"贸易商","filter_no":"1"}]

 

你可能感兴趣的:(jmeter)