简体   繁体   English

在JMeter中处理JSON响应

[英]Processing JSON response in JMeter

I am reading JSON data from HTTP response and I need to put some data which is extracted by JSONPath. 我正在从HTTP响应中读取JSON数据,我需要放入一些由JSONPath提取的数据。 Data I want is OK, it's showing while debugging, but I cannot insert it into other JSON object which I need to modify. 我想要的数据是可以的,它在调试时显示,但是我无法将其插入需要修改的其他JSON对象中。

Problem is I don't know how to insert new elements into JSON array and no matter what I try, I get error "The field XXX cannot be accessed". 问题是我不知道如何将新元素插入JSON数组,无论我尝试什么,都会收到错误消息“无法访问字段XXX”。 I am new to JSON and would appreciate some suggestions. 我是JSON新手,不胜感激一些建议。

JSON is something like: JSON类似于:

 {
  "background": null,
  "childInfos": [

  ],
  "claimScope": 2,
  "customAttributeInfos": [

  ],
  "dueDate": 1459461540000,
  "instructions": null,
  "name": "Client_1-23456",
  "owners": [
    "4YESyxwCtA2YBncmM+tnEU5Ze6Fev8K3"
  ],
  "priority": 1,
  "referenceFilesInfos": [    

   ]

   ...

  "batchInfos": [
    {
      "name": "Batch1",
      "targetFormat": "TXML",
      "workflowTicket": "4YESyxwCtA3PqFg+3vJ6nE5Ze6Fev8K3",
      "targetLanguageInfos": [
        {
          "targetLanguageLocale": "de-DE",
          "dueDate": 1459461540000,
          "dueDateInfos": [
            {
              "phaseName": "AAAAAAAA",
              "dueDate": 1458645663900
            },
            {
              "phaseName": "BBBBBBB",
              "dueDate": 1459098928400
            },
            {
              "phaseName": "CCCCCC",
              "dueDate": 1459461540000
            }
          ],
          "organizationTmTicket": null
        }
      ],
      "fileInfos": [

      ]

Now, I am dynamically getting objects which should be placed into existing fileInfos array. 现在,我正在动态获取应放入现有fileInfos数组中的对象。

I have tried things like via BeanShell: 我已经尝试通过BeanShell这样的事情:

JSONObject fajl = new JSONObject();     

fajl.put("repositoryTicket",vars.get("repTicket"));
fajl.put("name",vars.get("fileName"));
fajl.put("fileTargetFormat","TXML");
fajl.put("fileFormatTicket","4YESyxwCtA2glxeFIbqVOwNwQhim05Uq");
fajl.put("fileFormatName","Word");

String CEO = vars.get("CEO");
JSONObject pom = new JSONObject(CEO); - note1
pom.batchInfos[0].fileInfos[i-1].push(fajl); - note2

note1 - CEO is parsed JSON which I need for next request. note1-CEO被解析为下一个请求所需要的JSON。

note2 - I have counter which tracks number of elements processed. 注意2-我有一个计数器,它跟踪处理的元素数。 I have also tried this without counter. 我也尝试了这个没有计数器。

Object fajl is good, I know that, but how can I insert it into that array? 我知道对象fajl很好,但是如何将其插入该数组中?

Try the following code. 请尝试以下代码。 you will be able to add the object in array 您将能够在数组中添加对象

String jsonDataString = "{\"background\":null,\"childInfos\":[],\"claimScope\":2,\"customAttributeInfos\":[],\"dueDate\":1459461540000,\"instructions\":null,\"name\":\"Client_1-23456\",\"owners\":[\"4YESyxwCtA2YBncmM+tnEU5Ze6Fev8K3\"],\"priority\":1,\"referenceFilesInfos\":[],\"batchInfos\":[{\"name\":\"Batch1\",\"targetFormat\":\"TXML\",\"workflowTicket\":\"4YESyxwCtA3PqFg+3vJ6nE5Ze6Fev8K3\",\"targetLanguageInfos\":[{\"targetLanguageLocale\":\"de-DE\",\"dueDate\":1459461540000,\"dueDateInfos\":[{\"phaseName\":\"AAAAAAAA\",\"dueDate\":1458645663900},{\"phaseName\":\"BBBBBBB\",\"dueDate\":1459098928400},{\"phaseName\":\"CCCCCC\",\"dueDate\":1459461540000}],\"organizationTmTicket\":null}],\"fileInfos\":[]}]}";
            JSONObject mainObject = new JSONObject(jsonDataString);
            JSONObject fajl = new JSONObject();
            JSONArray list = new JSONArray();
            JSONArray batchInfos = mainObject.getJSONArray("batchInfos");
            JSONObject obj = batchInfos.getJSONObject(0);
            JSONArray fileInfos = obj.getJSONArray("fileInfos");
            for(int i=0;i<3;i++){
                fajl.put("repositoryTicket", i);
                fajl.put("name", i);
                fajl.put("fileTargetFormat", "TXML");
                fajl.put("fileFormatTicket", "4YESyxwCtA2glxeFIbqVOwNwQhim05Uq");
                fajl.put("fileFormatName", "Word");
                fileInfos.put(fajl);
            }
            System.out.println(mainObject);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM