Json returns each character as a separate object?

I have a json object that I load from wordpress using the JSON API plugin. When I load a json object and try to deduce it from it, it seems that it considers each individual symbol as its own object, so the loop returns me a couple of thousand objects with an element in it, which is the only symbol. This is my first time using json so idk if I don't see a step here. this is the code i'm still using.

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 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"
}

so instead of giving me β€œstatus” as the key and β€œok” as the value, I get β€œs” as an object with index 0, which has the value β€œs” for every single character in the json object. Any help on this would be appreciated.

+4
source share
4 answers

You need to set dataType:json in the $ .ajax () request so that jQuery converts the JSON string into a JavaScript object for processing as such. You are currently using application/json , which is a mime type and not a valid value for this field in a jQuery request.

+7
source

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

+1
source

Use the jQuery function:

 data = $.parseJSON(data); 

before using $ .each.

0
source

As I solved this in my AngularJS application, this is to send the response from the server (I use Node Express) as a JavaScript object, and not as a string. Nothing worked for me.

 var response = {}; response.error = "Error message"; res.send(response); 
0
source

Source: https://habr.com/ru/post/1337844/


All Articles