Home > Enterprise >  Representing request array in Java
Representing request array in Java

Time:11-09

I have the following request

{
"attachments":[
    {
        "fields":[
            {
                "value":"Testing value"
            }
        ]
    }
],
"channel":"testing",
"value":"Testing value"

}

and want to make it as a request in Java, but I am struggling how to represent it and use it. So far I've done this

public class RequestTest {
    ArrayList<ArrayList<String>> attachments = new ArrayList<ArrayList<String>>();
    ArrayList<String> fields = new ArrayList<String>();
    private String channel;
    private String value;
}

But I am not sure how to put the value and how to call it after that.

CodePudding user response:

You have to create an object structure like:

public class RequestTest {
    ArrayList<Attachment> attachments = new ArrayList<>();
    private String channel;
    private String value;
}

public class Attachement {
    ArrayList<Field> fields = new ArrayList<>();
}

public class Field {
  String value;
}     

Then you can use jackson or gson to create the json string you expect.

CodePudding user response:

If you want to present the request in Java class, then you should create a class named "Field" and class named "Attachment".

    public class Field{
        private String value;
    }

    public class Attachment{
        private List<Field> fields;
    }

    public class RequestTest{
        private List<Attachment> attachments;
        private String channel;
        private String value;
    }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

If you want to present it with JsonObject, you can refer to JsonObject

  • Related