0

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"}]
6
  • 1
    Does the console log any errors? Please specify the problem. Commented Oct 2, 2014 at 8:16
  • for single rows it works but for multiple rows it gives following alert [object object] Commented Oct 2, 2014 at 8:17
  • The answer of sakir should fix this. Commented Oct 2, 2014 at 8:18
  • Getting [object object] doesn't have to mean something bad. Try console.log(x) instead of alert(x). Commented Oct 2, 2014 at 8:19
  • not working console.log(x) Commented Oct 2, 2014 at 8:28

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Since when can you execute code snippets on stackoverflow? That's a nice feature. Thank you for that. +1 therefore
0

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.

Comments

0

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

3 Comments

in your case yourjsondatahere =res
not working success: function(res) { var json = $.parseJSON(res); $(json).each(function(i,val){ $.each(val,function(k,v){ console.log(k+" : "+ v); }); }); func(res); },
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.