Kavya Shree Kavya Shree - 4 years ago 165
Javascript Question

Convert Json response object to string using JQuery

This is the JSON response getting from webservice URL .
JSON:

{
"data":[
{
"outputQty":"2",
"orderSummaries":[],
"categoryName":"test",
"itemId":1,
"recipes":[],
"name":"Laddu"
},
{
"outputQty":"2",
"orderSummaries":[],
"categoryName":"Badam Pista Mundiri",
"itemId":2,
"recipes":[],
"name":"Barpi"
}
]
}


I tried to get JSON values in html page .So that I tried as like below:

$.ajax({
type: "GET",
url: "http://tomcatworkbench.com/Catering2/secured/getAllItems",
dataType: "json",
success: function(response) {
$.each(response, function(idx, obj) {
console.log(obj);
});
}
});


or

$.getJSON("http://tomcatworkbench.com/Catering2/secured/getAllItems", function(data){
$.each(data, function (index, value) {
console.log(value);
});
});


Both way returns object only I can't get string values.Please anyone help me to get out this issue.Thanks in advance.

Answer Source

just enumerate response.data

$.ajax({  
       type: "GET",  
       url: "http://tomcatworkbench.com/Catering2/secured/getAllItems",  
       dataType: "json",  
       success: function(response) {
           response.data.forEach(function(item) {
               console.log(item.categoryName);
           });

           var firstItem = response.data[0];                                                 
           console.log(firstItem.categoryName);

           var allCategoryNames = response.data.map(function(item) {
               return item.categoryName;
           });
           console.log(allCategoryNames);

           var allCategoryFirstNames = response.data.map(function(item) {
               return item.categoryName.split(' ')[0];
           });
           console.log(allCategoryFirstNames);
       }
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download