简体   繁体   English

在ajax调用之后将数据从PHP API转换为JSON对象

[英]Convert data from a PHP API to JSON object after ajax call

I have an api that returns the data in the following format after I call it using jQuery ajax: 使用jQuery ajax调用后,我有一个api以以下格式返回数据:

API Call API调用

$.get("displayapi.php",function(data){
        var json=data;
        console.log(json);});

Log in console 登录控制台

{
"listing": {
    "id": "7",
    "name": "Nina Randy",
    "product": "Studio Apartment for Rent ",
    "email": "n@gwmail.gwu.edu",
    "phone": "2147483647",
    "des": "Apartment near Foggy Bottom metro station for rent. Available from     May 2016. Please contact for more details",
    "cost": "3000",
    "category": "apartments",
    "date": "2016-04-24",
    "pic": "unnamed.jpg"
}
}{
"listing": {
    "id": "6",
    "name": "Jay Sean",
    "product": "Parking Spot near Gelman Library",
    "email": "jsean@gwmail.gwu.edu",
    "phone": "2147483647",
    "des": "Parking spot located near foggy bottom metro station for sharing. Please contact for availability. ",
    "cost": "1000",
    "category": "parking",
    "date": "2016-04-18",
    "pic": "1002240-13-20160117082202.jpeg"
} 
}

It displays the content in an Object format but is not really an Object. 它以对象格式显示内容,但实际上不是对象。 How can I convert it to JSON object so that I can access individual properties of the object. 如何将其转换为JSON对象,以便可以访问对象的各个属性。

You COULD just call the JSON.parse() on the string returned by php like so: 您可以只对php返回的字符串调用JSON.parse(),如下所示:

$.get("displayapi.php",function(data){
        var dataObject = JSON.parse(data);
});

But since your return value is not a valid JSON - and you really can't make it one - try the following approach: 但是,由于您的返回值不是有效的JSON-而且您真的做不到,请尝试以下方法:

$.get("displayapi.php",function(data){
        var dataObject = eval(data);
});

You should parse your response data to javascript json object like below; 您应该将响应数据解析为javascript json对象,如下所示;

$.get("displayapi.php",function(data){
    var json=data;
    var obj = JSON.parse(data);
    //iterate lsiting....
    $.each(obj.listing, function(i){
        console.log(obj.listing[i].name);
    })
});

jQuery has a native function called $.parseJSON() that will convert the JSON string to an object. jQuery有一个名为$ .parseJSON()的本机函数,它将JSON字符串转换为对象。

http://api.jquery.com/jquery.parsejson/ http://api.jquery.com/jquery.parsejson/

Example

$.get("displayapi.php",function(data){
    var json=data;
    console.log(json);
    var JSONdata = $.parseJSON(json);
    console.log(JSONdata.listing.id);
});

If you check the response header on console, you can see Content-Type set as "text/html;" 如果您在控制台上检查响应头,则可以看到Content-Type设置为“ text / html;”。 that is why you are getting problem in parsing response. 这就是为什么您在解析响应时遇到问题。

First, use $.getJSON instead of $.get 首先,使用$.getJSON代替$.get

Can you set proper Content type in your displayapi.php as well. 您是否也可以在displayapi.php中设置适当的内容类型。 Eg: 例如:

<?php
 $data_array = array(); // The array you want to serialize
 header('Content-Type: application/json');
 echo json_encode($data_array);
?>

This way you can avoid parsing the response again using javascript. 这样,您可以避免使用javascript再次解析响应。 Now you can check the console again and can see that Content-Type set as "application/json" 现在,您可以再次检查控制台,可以看到Content-Type设置为“ application / json”

Please test and let me know if this is working. 请进行测试,并让我知道是否可行。

I am suggesting yo use $.getJSON() method for get JSON data.. jquery take care about parsing JSON data with this method 我建议您使用$.getJSON()方法获取JSON数据$.getJSON()注意使用此方法解析JSON数据

$.getJSON("displayapi.php",function(json){
    $.each(json.listing, function(i){
        console.log(obj.listing[i].name);
    })
});

refer $.getJSON() method 参考$ .getJSON()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM