JSON response parsing sometimes array sometimes text -


I have two services: register and search with similar responses:

  { "N", "gender": "STDCode": "My Name", "Mobile Number": 9876543210, "Other Phone Number": [1111111111, 2222222222, 3333333333], "Language": " M "," However, the responses are identical, although the claim to the Jmter is slightly different for both.  

I am using JsonSlurper to parse as a result

  import groovy.jso N * Def slurper = new JsonSlurper () Def result = slurper.parseText (prev.getResponseDataAsString ())  

I used the feedback successfully used

  Applys the result. Name == "Meranam" // With the result of successful search fails with register. Name [0] == "My name" // If successful with search fails to register  

Just wanted to know why I have to use the array structure for a while, without the array structure [0] works? Did I not find anything using the parsing function or the way the coded coded way?

Your two responses no are a minor difference with your code < Code> result.farmerProfile.Name is a list such as (note [{... ...] there):

  import groovy Json Def = slurper = new JsonSlurper () Diff result = slurper.parseText ('' '\ "[[" STD code ":" 022 "," name ":" mimam "," mobile number ": 9876543210," Other phone numbers ": [1111111111, 2222222222, 3333333333]," language ":" n "," gender ":" m "," address ":" ...... "}] '' ') println result.Name // [MyName] println result. Name [0] // MyName println result *. [Meranam] Printline Results *. [0] // MyName  

The reason is the contained dispersion operator (note < Code> * name ). So you'll most likely have a in a list of Farmer Profile and the other . Which makes sense, because a search will enable me to return more than one.


Comments