最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Json returns every character as a separate object? - Stack Overflow

programmeradmin1浏览0评论

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: '/?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);
});

}

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\/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": ""
},
"ments": [],
"attachments": [],
"ment_count": 0,
"ment_status": "open"
}

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.

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./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);
});

}

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.\/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": ""
},
"ments": [],
"attachments": [],
"ment_count": 0,
"ment_status": "open"
}

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.

Share Improve this question edited Jan 31, 2011 at 20:15 Kyle Trauberman 25.7k13 gold badges87 silver badges124 bronze badges asked Jan 31, 2011 at 20:12 KobbyKobby 5904 silver badges13 bronze badges 1
  • FYI - when you want to show code fragments like that JSON stuff, paste it into the edit box, select it, then click the "{}" widget above the edit box. – Pointy Commented Jan 31, 2011 at 20:37
Add a ment  | 

4 Answers 4

Reset to default 9

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.

In your case you can even try data = eval(data) , this javascript statement should convert your string to json object.

Use the Jquery function:

data = $.parseJSON(data);

before using $.each.

The way I solved it in my AngularJS app is by sending the response from the server (I'm using Node Express) as a JavaScript object, rather than as a string. Nothing else worked for me.

var response = {};
response.error = "Error message";
res.send(response);
发布评论

评论列表(0)

  1. 暂无评论