How to structure Multiple object and array of JSON in java in beanshell and review the output

泄露秘密 提交于 2020-01-03 05:33:13

问题


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

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"
        }
    ]
}

The tried code is :

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

}

The output is not pasted in the beanshell sampler


回答1:


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

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

}


来源:https://stackoverflow.com/questions/56001955/how-to-structure-multiple-object-and-array-of-json-in-java-in-beanshell-and-revi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!