Jahangir Ansari
Jahangir Ansari

Reputation: 21

Parse json in jquery and java

Hi i am trying to make code for parse json in jquery and command comes from java controller and i didn't get solution

urlAddCountry="countries/countrylist";
getAjaxCountry(urlAddCountry, CountryDetails, true,true);


function CountryDetails(res)
{
    alert(res);
}

function getAjaxCountry(urlAddCountry, func, isToken,isContentHeader)
{

    var url=serviceURL + urlAdd;
    $.ajax({
        url: url,
        type: "POST",
        //isasync: isAsync,
        contentType : "application/json",
        beforeSend:function(xhr){
            if(isContentHeader){
                xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            } 
            if(isToken){
                xhr.setRequestHeader("tokenId",sessionStorage.tokenID);
            }
        },
        success: function(res) {
            alert(res);
            func(res);
        },
        error : function(err) {
            func(err);
            alert("ERROR:STATUS- " + err.status + "; RESPONSETEXT- " + err.responseText + "; STATUSTEXT- " + err.statusText);
            //return null;
        }
    });

}

[{"entry_id":1,"countryName":"India","phoneCode":"91","countryCode":"IN","isBlackListedForPGTran":"0"},{"entry_id":2,"countryName":"Timor-Leste","phoneCode":"NULL","countryCode":"TL","isBlackListedForPGTran":"0"}]

Upvotes: 0

Views: 132

Answers (3)

sakir
sakir

Reputation: 3502

You can use the following code in your success callback:

var json = $.parseJSON(yourjsondatahere);
$(json).each(function(i,val){
    $.each(val,function(k,v){
        console.log(k+" : "+ v);     
    });
});

Upvotes: 0

T J
T J

Reputation: 43156

According to what i understood from comments, you can use JSON.stringify to stringify your json so that you can see it.

var jsonArray = [{"entry_id":1,"countryName":"India","phoneCode":"91","countryCode":"IN","isBlackListedForPGTran":"0"},{"entry_id":2,"countryName":"Timor-Leste","phoneCode":"NULL","countryCode":"TL","isBlackListedForPGTran":"0"}]

alert(JSON.stringify(jsonArray));

Parsing means, to convert a json string into an object. If you alert an object, you'll see corresponding structure like [object object] as you mentioned in comments.

Upvotes: 1

Darshan Lila
Darshan Lila

Reputation: 5868

Try following using jquery

var json = jQuery.parseJSON(res);
    $.each(json, function(k, v) {
        console.log(k+' :: '+v);
    });

While in Java you need to use Json Librabry.

For download visit this link and for tutorial visit this link.

Upvotes: 0

Related Questions