Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

jmeter - Parsing json objects(multiple) using groovy

I have below response as JSON:

{  
 "id":27,
 "merchant_id":"39",
 "title":"Shorts",
 "subtitle":null,
 "price":100,
 "description":null,
 "images":[  

  ],
  "image_thumbs":[  

  ],
  "options":[  
    {  
     "code":"size",
     "label":"Size",
     "extra_info":"",
     "values":[  
        {  
           "label":"Small",
           "value":"4"
        },
        {  
           "label":"Medium",
           "value":"5"
        }
     ]
     }
  ],
  "options_available":[  
    {  
     "combination":[  
        {  
           "code":"size",
           "value":"Small"
        }
     ]
     },
    {  
     "combination":[  
        {  
           "code":"size",
           "value":"Medium"
        }
     ]
     }
  ],
  "custom_options":[  

  ]
}

Or this response:

{  
 "id":37,
 "merchant_id":"39",
 "title":"Parker Pens",
 "subtitle":null,
 "price":1000,
 "description":null,
 "images":[  

  ],
  "image_thumbs":[  

  ],
  "options":[  
    {  
     "code":"color",
     "label":"Color",
     "extra_info":"",
     "values":[  
        {  
           "label":"Red",
           "value":"8"
        },
        {  
           "label":"Yellow",
           "value":"9"
        },
        {  
           "label":"Pink",
           "value":"10"
        }
     ]
      },
    {  
     "code":"size",
     "label":"Size",
     "extra_info":"",
     "values":[  
        {  
           "label":"Small",
           "value":"4"
        },
        {  
           "label":"Medium",
           "value":"5"
        },
        {  
           "label":"Large",
           "value":"6"
        }
       ]
     }
 ],
  "options_available":[  
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Red"
        },
        {  
           "code":"size",
           "value":"Small"
        }
     ]
     },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Red"
        },
        {  
           "code":"size",
           "value":"Medium"
        }
       ]
     },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Red"
        },
        {  
           "code":"size",
           "value":"Large"
        }
     ]
    },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Yellow"
        },
        {  
           "code":"size",
           "value":"Small"
        }
     ]
    },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Yellow"
        },
        {  
           "code":"size",
           "value":"Medium"
        }
     ]
    },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Yellow"
        },
        {  
           "code":"size",
           "value":"Large"
        }
     ]
    },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Pink"
        },
        {  
           "code":"size",
           "value":"Small"
        }
     ]
    },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Pink"
        },
        {  
           "code":"size",
           "value":"Medium"
        }
       ]
    },
    {  
     "combination":[  
        {  
           "code":"color",
           "value":"Pink"
        },
        {  
           "code":"size",
           "value":"Large"
        }
       ]
    }
 ],
 "custom_options":[  

 ]
 }

I need to check the Options array.

If its count is 1 , then i need to select randomly Values array and get values inside it.

If Options array count is 2 , then i need to parse those two Values array and randomly select Values array from those two and get values inside it.

Sample code:

import com.jayway.jsonpath.JsonPath
import org.apache.commons.lang3.RandomUtils
import org.apache.jmeter.samplers.SampleResult
import java.util.Random;

def testArray = []
def new_array = []
def options = JsonPath.read(prev.getResponseDataAsString(), '$.options')
def options_length = options.size()
log.info('length:' +options_length)
Random r= new Random();
def minimum = 0
if(options_length > 1)
{
for ( int a = 0 ; a < options_length ; a++ )
{
    testArray.add(options[a].get("code"));
    def values_length = options[a].get("values").size();
    log.info('values_length:' +values_length);
    new_array.add( options[a].get("values"));
    log.info('new_array:' +new_array[a]);
    def array_counting= new_array[a].size();
    def random_value = r.nextInt(values_length - minimum )  + minimum;
    log.info('random_value:' +random_value);
    def final_value=new_array[random_value].get(value);
    log.info('final_value:' +final_value);
    }

  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...