简体   繁体   中英

how to return data in json format to javascript ajax call

I have the following method:

public String getUTResult() throws IOException {

     BuildResultParser bp = new BuildResultParser();
     BuildResultBean b = bp.getreadFile("C:\\bc.txt");

     String str = b.getuTresult();

     return str;

Now str variable contains the value as: [0,5,5]

Now I need to pass this value to ajax call in javascript in following format:

unittest
   {
     fail:0
     pass:5
     total:5
   }

Actually in javascript, I need this data in array format so that I can access each value and do some processing.

Read this : http://hmkcode.com/java-servlet-send-receive-json-using-jquery-ajax/ or any example of converting data to json format.

If your requirement is limited to the example, u can have another function which takes str and creates json data out of it, in the format required by you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM