Expand my Community achievements bar.

SOLVED

How to get key set of serialized json object in servlet for post form processing

Avatar

Level 8

Hello,

 

could someone please provide inputs if there a possibility for  getting all the field names as

 keyset so that need not hardcode the formDataObject keys as per below??

 

 

1>

I have a form json data and have created 

public class FormRequestJsonData {

    @SerializedName("field1")

    @Expose

    private String field1;

 

@SerializedName("field2")

    @Expose

    private String field2;

 

....

@SerializedName("field20")

    @Expose

    private String field20;

}

  2>for the JsonObject  content

{
    "field1" : "string",
    "field2" : "string",

....
"field20" : "string" }

 

I have about 20 fields and on serializing like below 

FormRequestJsonData formDataObject = new GsonBuilder().create().fromJson(
                content, FormRequestJsonData.class);

 

I will need to some dynamic processing for each field in formDataObject with xssapi validation.

So need to get each key and need to write  xssapi.filter code.

Is there a possibility for the getting all the field names as keyset so that need not hardcode the formDataObject keys

currently doing

if("field1"){

xssapi.filterhtml(formDataObject.getField1());

}

if("field2"){

xssapi.filterhtml(formDataObject.getField2());

}

Regards,

Srinivas

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

There is a way to do that, 

you can map String response to map. this will give you key and value in map format

 

private List<Map<String, Object>> fragment = new ArrayList<>();


Arun Patidar

View solution in original post

2 Replies

Avatar

Level 8

Hello,

 

Any inputs from anyone on this

 

Thanks

Avatar

Correct answer by
Community Advisor

There is a way to do that, 

you can map String response to map. this will give you key and value in map format

 

private List<Map<String, Object>> fragment = new ArrayList<>();


Arun Patidar