如何在beanshell中的Java中构造JSON的多个对象和数组并查看输出 [英] How to structure Multiple object and array of JSON in java in beanshell and review the output

查看:189
本文介绍了如何在beanshell中的Java中构造JSON的多个对象和数组并查看输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is JSON file. I want make java can produce like this json. Just ignore the value, What i want is the structure of the json. I am creating it in the beanshell sampler

这是我在Beanshell采样器中尝试过的

This i have tried in the beanshell sampler

"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采样器中

The output is not pasted in the beanshell sampler

推荐答案

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

You can create object structure to look like your json and than use Gson to serialize you objects int json.

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

Example (i used lombok to remove bulk code):

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));
    }

}

这篇关于如何在beanshell中的Java中构造JSON的多个对象和数组并查看输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆