我在服务器的响应中获取JSON数据。但是我如何解析要在弹出窗口中显示的数据

时间:2011-06-16 22:18:12

标签: jquery json jsonp

我从服务器获取json数据作为响应。我想解析这个JSON数据并将所有数据嵌入到弹出窗口中。

var on_show_info = function() {
                alert("aa");
                request_meta_info = $.ajax({
                url: data + info.id,
                type: 'GET',
                async: false,
                dataType: "jsonp",
                success: data,
                error: error    
        });
    };

var data = function(data, text_status, XMLHttpRequest) {

    alert(data);
    var html = '';
    var info = {};


    here I want that JSON parsing code as I am getting response back from the server in data field. Any help will be appreciated and I want to embed those parse data in to a popup window as shown below by this command `($('#popup-meta .popup-content').html(html);)`


    });

    $('#popup-meta').removeClass('waiting');
    $('#popup-meta .popup-content').html(html);
};

这是我作为JSON数据从服务器返回的响应,仅用于示例..

jsonp13082({"responseHeader":{"status":0,"Time":3,"params":{"json.wrf":"jsonp13082","wt":"json","q":"8377"}},"response":{"numFound":1,"start":0,"docs":[{"key":"83779616","number":"080","name":"Designated","name":"Non ","number":"27837","date":"2010-08-24T07:00:00Z","name":"Canada","name":"Application","title":"collision detection","date":"2008-03-03T08:00:00Z","id":"414","code":"CA","date":"2009-03-03T08:00:00Z","name":"Michael Henry","mgr_name":"William Henry","id":"79616","name":"oen","claims":"74","date":"2012-03-03T08:00:00Z","claims":"8","url":"","inventors":["D.","rshi","Pa"],"guid":["23","26","25"],"towners":["XYZ"],"inventors":["D","name2","name3"],"owners":["XYZ"]}]}})

4 个答案:

答案 0 :(得分:1)

 var dataObj = jQuery.parseJSON( jsonStr );

有关jQuery(包括在内)和将jsons转换为数据格式的更多信息,请参阅链接。

http://api.jquery.com/jQuery.parseJSON/

您如何使用数据进行显示取决于您以及您获得的数据。

您可以使用jQuery native对话框显示弹出窗口

dialog in jQuery

您可以在其中执行for循环,通过HTML格式(可能使用HTML表格)将每个参数转换为显示。

function tablefy( object ) {
var retStr = "";
var typeSet = typeof(object);
if( typeSet == 'object' ) {
    retStr += "<table>"
    for( x in object ) {
        retStr += "<tr>";
        retStr += "<td>";
        if( typeof( x ) == "string" ) {
            retStr += x;
        } else {
            retStr += x.toString();
        }
        retStr += "</td><td>";
        retStr += tablefy( object[x] );
        retStr += "</td>"
        retStr += "</tr>"
    }
    retStr += "</table>"
} else {
    //just dumps it raw
    if( typeSet == 'String' ) {
        return object;
    } 

    if( typeSet == 'number' ) {
        return object.toString();
    }

    if( typeSet == 'boolean') {
        if( object == true ) {
            return 'true';
        } else {
            return 'false';
        }
    }

    return 'null';

    }
}
return retStr;
}

答案 1 :(得分:0)

我认为如果您将请求的数据类型从jsonp更新为json,那么响应处理程序中的数据对象应该已经被解析,所以

alert(data.name);

应该有效(如果返回的对象符合上面的例子)

编辑:这个答案基于OP的指示,即响应是json,但响应似乎是jsonp

答案 2 :(得分:0)

如果您只想将其显示为纯文本,则必须进行转换。尝试使用http://code.google.com/p/jquery-json/

$。的toJSON(数据)

答案 3 :(得分:0)

因此,您无法使用警告框显示它,因为一旦它到达客户端,它就会自动变为javascript对象。警报毫无结果。

尝试改为

console.log (data)

并查看控制台窗口中的内容(即 - f12,chrome - ctrl shift j)