The string string of JSON converts to a Java list-list object _java

Source: Internet
Author: User
Tags convert json to string convert string to json string to json

In front:
1. If JSON is a list object conversion, you can traverse JSON directly and read the data.
2. If it is necessary to convert the front End list object to JSON to the background, param is an ajax parameter, then the conversion looks like this:

var jsonstr = json.stringify (list); 
var param= {}; 
PARAM.JSONSTR=JSONSTR; 

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

list<t> list = new arraylist<t> (); 
Jsonarray Jsonarray = Jsonarray.fromobject (str);//convert string to JSON 
list = jsonarray.tolist (jsonarray,t); Here's T is class<t> 

2. Convert list to JSON

Jsonarray JSON = Jsonarray.fromobject (object); 
String str = json.tostring ();//convert JSON to string 

eg
1. form a list of Answer objects based on 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> Answer 
     
    List = 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; /** * Converts a JSON string to list * @param props * @return/Public Static list<answer> converanswerformstring (String Answer) {if (Answer = = NULL | | answer.equals (")) RET 
 
    Urn new ArrayList (); 
    Jsonarray Jsonarray = Jsonarray.fromobject (answer); 
     
    list<answer> list = (list) jsonarray.tocollection (Jsonarray, Answer.class); 
  return list; 
 }

2. Generate a JSON string from a list of Answer objects, based on information entered by the client page user

  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 (); 
  } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.