嗨,我正在尝试编写在 jquery 中解析 json 的代码,命令来自 java Controller ,但我没有得到解决方案
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"}]
最佳答案
根据我从评论中了解到的,您可以使用JSON.stringify
将您的 json 字符串化以便您可以看到它。
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));
解析的意思是,将json字符串转换为对象。如果您提醒某个对象,您将看到相应的结构,如您在评论中提到的 [object object]
。
关于java - jquery和java中解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156731/