Conversion between JSON's String string and Java's List list objects

  • 2020-05-09 18:38:19
  • OfStack

On the front end:
1. If json is converted by List objects, json can be traversed directly to read data.
2. If it is necessary to convert the front-end List object into json and pass it to the background, and param is the parameter of ajax, then the conversion is as follows:


var jsonStr = JSON.stringify(list); 
var param= {}; 
param.jsonStr=jsonStr; 

In the background:
1. Convert String to List(str to list)


List<T> list = new ArrayList<T>(); 
JSONArray jsonArray = JSONArray.fromObject(str);// the String convert json 
list = JSONArray.toList(jsonArray,t);// Here, t is Class<T> 

2. Convert List to json


JSONArray json = JSONArray.fromObject(object); 
String str = json.toString();// the json convert String 

eg:
1.   forms List   of Answer objects according to the information entered by the page user


/** 
  * @param answers 
  * @param question_ids 
  * @param types 
  * @return 
  */ 
 private List<Answer> toAnswerList(String[] studenAnswers, int[] question_ids, 
   int[] types,int[] scores) { 
  List<Answer> answerList = new ArrayList<Answer>(); 
   
  if(studenAnswers!=null && question_ids!= null && types!= null&& scores!= null){ 
   for (int i = 0; i < studenAnswers.length; i++) { 
     
    Answer answer = new Answer(); 
    String studenAnswer = studenAnswers[i]; 
    int type = types[i]; 
    int question_id = question_ids[i]; 
    int score = scores[i]; 
   
     
    answer.setQuestion_id(question_id); 
    answer.setScore(score); 
    answer.setStudenAnswer(studenAnswer); 
    answer.setType(type); 
     
    answerList.add(answer); 
   } 
  } 
  return answerList; 
 } 
 
 /** 
  *  will 1 a json String into a list 
  * @param props 
  * @return 
  */ 
 public static List<Answer> converAnswerFormString(String answer){ 
  if (answer == null || answer.equals("")) 
   return new ArrayList(); 
 
  JSONArray jsonArray = JSONArray.fromObject(answer); 
  List<Answer> list = (List) JSONArray.toCollection(jsonArray, 
    Answer.class); 
   
  return list; 
 } 

2.   generates an Json string from the List of an Answer object, which is an   generated from the information entered by the user on the client page


 public String getAnswerString(String[] studenAnswers, int[] question_ids, 
   int[] types,int[] scores) { 
  List list = toAnswerList(studenAnswers, question_ids, 
     types, scores); 
  JSONArray jsonarray = JSONArray.fromObject(list); 
 
  return jsonarray.toString(); 
 } 

 

PS: here are some more practical json online tools for your reference:

Online JSON code validation, validation, beautification, formatting tools:
http://tools.ofstack.com/code/json

JSON online formatting tool:
http://tools.ofstack.com/code/jsonformat

Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson

json code online formatting/beautification/compression/editing/conversion tools:
http://tools.ofstack.com/code/jsoncodeformat

Online json compression/escape tool:
http://tools.ofstack.com/code/json_yasuo_trans

C language style /HTML/CSS/json code formatting beautification tool:
http://tools.ofstack.com/code/ccode_html_css_json


Related articles: