Parsing an array of JSON objects

I am very new to using jQuery and Json. I have a servlet that returns a JSONObject array (basically a JSONArray).

I am trying to parse this array in JavaScript and here I ran into difficulties. I have a javascript variable "result var" that receives the result from the servlet, and I'm trying to parse it as the result of [0] .uniqueId, for example, to get the uniqueId value sent from the server. But that does not work.

The following is a Java servlet code snippet to show what is being sent to the javascript client.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
                    JSONArray arrayObj = new JSONArray();
             List<Folder> children =getFolders(request,response);
             Iterator itr = children.iterator();
             while(itr.hasNext())
             {
                 Folder folder = (Folder) itr.next();
                 obj = new JSONObject();
                 obj.put("uniqueId", folder.getUniqueId());
                 obj.put("folderName", folder.getFolderName());
                 obj.put("size", folder.getSize());
                 obj.put("modified", folder.getModified());

                 arrayObj.add(obj);
             }
            out = response.getWriter();
            response.setContentType("application/json");
            out.println(arrayObj);           
} 

This is a jQuery code snippet

$.ajax({
            url: 'getFolders',
            type: 'POST',
            data: 'uniqueID=' + uniqueID ,
            //console.log(data);
            success: function(result) {  
                //parse result
                        alert("JSON result "+ result[0].uniqueId);

                }//end success
    });

Please consult the analysis results.

Thanks Deepthi

+3
1

dataType , :

$.ajax({
              dataType: 'json',
              url: 'getFolders',
              type: 'POST',
              data: 'uniqueID=' + uniqueID ,
              //console.log(data);
              success: function(result) {  
              //parse result
                    alert("JSON result "+ result[0].uniqueId);

              }//end success
        });
+3

Source: https://habr.com/ru/post/1721598/


All Articles