Json将每个字符作为单独的对象返回? [英] Json returns every character as a separate object?
问题描述
我有一个json对象,我正在使用JSON API插件从wordpress加载。当我加载json对象并尝试注销它的部分时,似乎它将每个单个字符视为它自己的对象,因此循环返回我几千个对象,所有对象都是单个字符。这是我第一次使用json如此idk,如果我在这里错过了一步。这是我到目前为止使用的代码。
I have a json object that I'm loading from wordpress using the JSON API plugin. When I load the json object and try to log out the parts of it, it seems like it treats every single character as its own object so the loop returns me a couple thousand objects all with item in it which is a single character. This is my first time using json so idk if i'm missing a step here. this is the code I'm using so far.
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,
帖子:[
{
id:175,
type:post,
slug:home-page-slider,
url:http:\ / \ / testing.charter21.com \ / uncategorized\ / home-page-slider \ / ,
status:publish,
标题:主页滑块,
title_plain:主页滑块,
content:< p>此处的cImages是主页滑块中的图片,此框中的内容不会显示。仅作为指南。< \ / p> \ n,
摘录:cImag这里的es是主页滑块中的图像,此框中的内容不会显示。它仅作为指南。,
date:2011-01-25 10:40:25,
已修改:2011-01-25 10:40:25,
类别:[],
tags:[],
author:{
id:1,
slug:admin,
name:admin,
first_name:,
last_name:,
昵称:admin,
url: ,
description:
} ,
评论:[],
附件 :[],
comment_count:0,
comment_status:open
}
the json looks like this:
{
"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"
}
所以基本上不是给我状态作为一个键而ok作为一个值,我得到s作为一个obj对于json对象中的每个字符,索引为0,其值为s。任何有关此事的帮助将不胜感激。
so basically instead of giving me "status" as an key and "ok" as a value, i get "s" as an object with an index 0 that has a value of "s" for every single character in the json object. Any help on this matter would be appreciated.
推荐答案
您需要设置 dataType:json $在$ .ajax()请求中使用c $ c>,以便jQuery将JSON格式的字符串转换为JavaScript对象,以便您进行处理。您当前正在使用
application / json
这是一个mime类型,而不是jQuery请求中此字段的有效值。
You need to set dataType:json
in your $.ajax() request so that jQuery converts the JSON-formatted string into a JavaScript object for you to process as such. You're currently using application/json
which is a mime type, and not a valid value for this field in a jQuery request.
这篇关于Json将每个字符作为单独的对象返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!