如何在beanshell中的Java中构造JSON的多个对象和数组并查看输出

时间:2019-05-06 09:04:04

标签: java json jmeter beanshell

这是JSON文件。我想让java可以产生这样的json。只是忽略该值,我想要的是json的结构。 我在beanshell采样器中创建它

我已经在beanshell采样器中尝试过

"itemLines": {
    "itemLine": [
        {
            "bundleParentId": "",
            "id": "1",
            "itemType": "ART",
            "itemNo": "00258882",
            "requiredQty": "1",
            "unitOfMeasure": "Piece"
        },{
            "bundleParentId": "",
            "id": "2",
            "itemType": "ART",
            "itemNo": "20215877",
            "requiredQty": "1",
            "unitOfMeasure": "Piece"
        },
        {
            "bundleParentId": "",
            "id": "2",
            "itemType": "ART",
            "itemNo": "20215877",
            "requiredQty": "1",
            "unitOfMeasure": "Piece"
        }
    ]
}

尝试的代码是:

public void createJsonStructure() {

try
{
    JSONObject rootObject = new JSONObject();
    JSONArray articleArr = new JSONArray();
    String[] article_list = {"00258882", "70234185", "00258882"};
    log.info(article_list.length);  
    for (i=0;i<=article_list.length;i++)
    {
    JSONObject article_list= new JSONObject();
    article_list.put("id", "i+1");
    article_list.put("itemNo",article_list[i]);
    article_list.put("requiredQty", "1");
    articleArr.put(article_list);
    }
   log.info(articleArr);        
    rootObject.put("itemLines", articleArr);
    log.info("rootObject is"+rootObject.toString(4));
    props.put("JsonObjectoutput", rootObject.toString(4));        
   }
catch (Exception ex)
{
    ex.printStackTrace();
    log.info("notes");
}

}

输出未粘贴到beanshell采样器中

1 个答案:

答案 0 :(得分:0)

您可以创建对象结构使其看起来像json,然后使用Gson将对象序列化为json。

示例(我使用lombok删除了批量代码):

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Item {

    private String bundleParentId;
    private int id;
    private String itemType;
    private String itemNo;
    private int requiredQty;
    private String unitOfMeasure;

}
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;

@Data
@AllArgsConstructor
public class ItemLine {

    private List<Item> itemLine;

}
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ItemLines {

    private ItemLine itemLines;

}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

public class JsonTest {

    public static void main(String[] args) {
        Item item1 = new Item("", 1,"ART", "00258882", 1, "Piece");
        Item item2 = new Item("", 2,"ART", "20215877", 1, "Piece");
        Item item3 = new Item("", 2,"ART", "20215877", 1, "Piece");

        List<Item> items = new ArrayList<>();
        items.add(item1);
        items.add(item2);
        items.add(item3);

        ItemLine itemLine = new ItemLine(items);
        ItemLines itemLines = new ItemLines(itemLine);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        System.out.println(gson.toJson(itemLines));
    }

}