javascript - Json 将每个字符作为一个单独的对象返回?

标签 javascript jquery json wordpress

我有一个 json 对象,我正在使用 JSON API 插件从 wordpress 加载它。当我加载 json 对象并尝试注销它的部分时,它似乎将每个字符都视为它自己的对象,因此循环返回给我几千个对象,所有对象都包含项目,这是一个字符。这是我第一次使用 json,所以如果我在这里遗漏了一步,我就知道了。这是我目前使用的代码。

function getProjInfo(theId){ 
    $.ajax({// calling the ajax object of jquery
        type: "GET",// we are going to be getting info from this data source
        url: 'http://testing.charter21.com/api/get_post/?post_id='+ theId,//the datasource
        dataType: "application/json",
        success: function(data){
        parseJson(data);
    }, // what happens when it is successful at loading the XML 
    error: function(){
        alert("error");
    }   
    });

}//end of the function

function parseJson(inData){
    postInfo = inData;
    $.each(postInfo, function(index, value){
    console.log(this);
});

}

json 看起来像这样:
{
"status": "ok",
"count": 10,
"count_total": 19,
"pages": 2,
"posts": [
{
"id": 175,
"type": "post",
"slug": "home-page-slider",
"url": "http:\/\/testing.charter21.com\/uncategorized\/home-page-slider\/",
"status": "publish",
"title": "Home Page slider",
"title_plain": "Home Page slider",
"content": "<p>The cImages in here are the images that are in the slider on the home page this content in this box will not show up. Its only here as a guide.<\/p>\n",
"excerpt": "The cImages in here are the images that are in the slider on the home page this content in this box will not show up. Its only here as a guide.",
"date": "2011-01-25 10:40:25",
"modified": "2011-01-25 10:40:25",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "admin",
"url": "",
"description": ""
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "open"
}

所以基本上不是给我“status”作为键和“ok”作为值,我得到“s”作为一个索引为 0 的对象,json 中的每个字符都有一个值“s”目的。对此问题的任何帮助将不胜感激。

最佳答案

您需要在 $.ajax() 请求中设置 dataType:json,以便 jQuery 将 JSON 格式的字符串转换为 JavaScript 对象,以便您进行处理。您当前使用的 application/json 是一种 mime 类型,不是 jQuery 请求中该字段的有效值。

关于javascript - Json 将每个字符作为一个单独的对象返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4855583/

相关文章:

jquery - PDF.js - 在嵌入式 PDF 上使用搜索功能

javascript - 如何使用仅在运行时已知的变量名调用 JavaScript 函数?

javascript - 获取包含该函数的匿名变量的名称

javascript - 使用 JS 从不同的 html 文件访问 DOM

jquery - Jqplot 线图,具有悬停在线效果

javascript - 更新 'stacked columns' 图表 : update() or setData()? 上的 highcharts.js 数据

javascript - CSS3 - 移动 SVG 圆圈

android - JSONObject 无法转换为 JSONArray

javascript - 如何在node.js javascript文件中包含json文件

javascript - 如何在javascript中查找对象的indexof