java - jquery和java中解析json

标签 java jquery json

嗨,我正在尝试编写在 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/

相关文章:

java - opengl 旋转纹理偏离中心

java - 如何使用stream/ifPresent对java.util.Set中的每个项目执行操作?

java - Sharedpreferences 在 SetText #android 时抛出空指针

php - 将日期格式转换为mysql时间戳

JSON 到 XSLT : default XML node if JSON element does NOT exist

java - 验证请求正文中的日期

java - 通过两个进程访问Kafka Topic

javascript - 一个真正的循环 <div> 通过 css 或 js 手段

jquery - 在 ASP.NET MVC 3 中将参数从 JQuery 传递到 Controller 操作

java - 使用 Jackson 将 Map 转换为 JsonParser