0

I want to create a json payload like

   {
         "email":"dautpure@gmail.com",
         "validators": ["SyntaxValidator", "MXValidator", "ListDetectiveValidator"]
   }

I wrote the following code :


package com.forcelocker.loaddata;

import com.google.gson.JsonObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class validateEmail 
{     

 public static String CheckingURL = "h_ttps://abc.com/address/v1/validateEmail";
    private static final String Content_Type ="application/json";
    
    public static Boolean CheckEmail(String Email,String Token) throws UnirestException
    {
       Boolean IsmailOk=false;
       String Authorization = "Bearer "+Token;
       

       
       JsonObject payload = new JsonObject(); 
       payload.addProperty("email", Email);
        HttpResponse<String> response = Unirest.post(CheckingURL)
                                            .header("Content-Type", Content_Type)
                                            .header("Authorization", Authorization)
                                            .body(payload).asString();
               
        return IsmailOk;
    }
}

But I don't know how to put the validator in JSON which can hold a comma seperate values

any help would be great.

ShekharApp
  • 33
  • 1
  • 5

2 Answers2

0

what about

 payload.addProperty("validator", new String [] {"SyntaxValidator", "MXValidator", "ListDetectiveValidator"});
noname
  • 565
  • 6
  • 23
  • I get following error by adding above statement: no suitable method found for addProperty(String,String[]) method JsonObject.addProperty(String,String) is not applicable (argument mismatch; String[] cannot be converted to String) method JsonObject.addProperty(String,Number) is not applicable – ShekharApp Oct 17 '19 at 12:51
  • Can you use HashMap? If yes please check this https://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java – noname Oct 17 '19 at 12:52
  • Used hashmaps to solve the problem. noname i want to mark you as best answer but dont know here to do it – ShekharApp Oct 17 '19 at 13:44
0

The problem was solved by using hashmap as suggested by noname. then converting it to json string and using it as payload for api

Map<String,Object> map = new HashMap<>();
       String[] val = new String[] {"SyntaxValidator", "MXValidator", "ListDetectiveValidator"};
       map.put("email", Email);
       map.put("validators", val);
       
       ObjectMapper objectMapper = new ObjectMapper();
       String json = objectMapper.writeValueAsString(map);

Thanks noname

ShekharApp
  • 33
  • 1
  • 5