I am new in wordpress and using Json-Api in wordpress for web-services, and I have create my own service in json-api/controllers/core.php
file my function is:
<?php
public function get_blogs(){
global $json_api;
$categories = $json_api->introspector->get_categories($args);
echo "[";
foreach($categories as $cat){
if($cat->id == "5432"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5433"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5439"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5444"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5440"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5438"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5430"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5435"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5436"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5434"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5441"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5445"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5431"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5437"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "203"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5442"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5447"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "530"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}".",";
}
if($cat->id == "5443"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost,JSON_PRETTY_PRINT);
echo $catnow;
echo "}".",";
}
if($cat->id == "5448"){
$catPost = get_posts('cat="$cat->id"&posts_per_page=4');
echo "{".""category_title":"$cat->title"".","category_id":"$cat->id"".","posts":";
$catnow = json_encode($catPost);
echo $catnow;
echo "}";
}
}
echo "]";
}
After that when I am getting its output as I want ,one null is extra added in Json after finish my Json output "]null"
in last, Please help.
It seems highly likely there is another file being run after the one you’ve posted, that is responsible for echoing that
null
. It is worth looking into this.Having said that, rather than trying to format your own JSON output, you’re probably better creating data objects, and then encoding them with
json_encode()
.So, for instance, to take the last block of your code, it would look like
If the entire thing is written in this form, it should get rid of any formatting errors.