When sending json data to ajax...

Asked 1 years ago, Updated 1 years ago, 127 views

When sending json data to ajax, I'm sending you the data in the form below.

It's for dividing data by each key and list in Java. How should I receive it?

I'd appreciate your help.

[{"QNo":"1","qType":"1","qText":"aaaa"},{"QNo":"2","qType":"1","qText":"bbbb"},{"QNo":"3","qType":"2","qText":"ccccc","answerList":[{"answerNo":1,"answerText":"111"},{"answerNo":2,"answerText":"222"},{"answerNo":3,"answerText":"333"},{"answerNo":4,"answerText":"444"}]}]

jquery ajax java json gson

2022-09-22 14:26

2 Answers

If you match the field name of the class with the key value of json data, gson basically maps it. Below is a very simple example of parsing a single block, and I divided the classes by my standards.

class Question {
    private int QNo;
    private String qType;
    private String qText;
    private List<Answer> answerList;
    ...
}

class Answer {
    private int answerNo;
    private String answerText;;
    ...
}


public static void main(arguments[] args){
     String jsonData = "{"QNo":"1","qType":"1","qText":"aaaa"}";
     Gson gson = new Gson();
     Question question = gson.fromJson(jsonData , Question.class);   
}


2022-09-22 14:26

When sending a request query to the server group, it would be most efficient to send a variable that represents each key and list, and the server group refers to the variable value and sends it by key and list.


2022-09-22 14:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.