1

Right now , I am working on a project. I have to build some rest full api's for android application using Woo-Commerce CLIENT REST API.

Everything is working fine, but the data i'm getting is unnecessary. So can anybody tell me, how to overcome from this problem.

e.g. I am getting this data

{
product_categories: [
{
id: 8,
name: "Cakes",
slug: "cakes",
parent: 0,
description: "Love is like a good cake; you never know when it's coming, but you'd better eat it when it does!",
count: 11
},
{
id: 9,
name: "Breads",
slug: "breads",
parent: 0,
description: "All sorrows are less with bread. ",
count: 3
},
{
id: 10,
name: "Pastries",
slug: "pastries",
parent: 0,
description: "I'm not a vegetarian! I'm a pastries-ian!",
count: 6
}

but i do not want slug,parent, description parameters.

Thanks in advance.

Kulwinder Kohli
  • 41
  • 1
  • 3
  • 8
  • maybe this link helps you? http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object – cgee Aug 17 '15 at 11:55
  • I want to do it from server side. There are lots of unnecessary parameters , to remove like this is huge headache. – Kulwinder Kohli Aug 17 '15 at 11:58

1 Answers1

0

Your Problems is " " missing in key Ex: "ID", "name" and
You use this function json_pretty() for json format.

function json_pretty($json, $html = false) {
    $out = ''; $nl = "\n"; $cnt = 0; $tab = 4; $len = strlen($json); $space = ' ';
    if($html) {
        $space = ' ';
        $nl = '<, $html = false)br/>';
    }
    $k = strlen($space)?strlen($space):1;
    for ($i=0; $i<=$len; $i++) {
        $char = substr($json, $i, 1);
        if($char == '}' || $char == ']') {
            $cnt --;
            $out .= $nl . str_pad('', ($tab * $cnt * $k), $space);
        } else if($char == '{' || $char == '[') {
            $cnt ++;
        }
        $out .= $char;
        if($char == ',' || $char == '{' || $char == '[') {
            $out .= $nl . str_pad('', ($tab * $cnt * $k), $space);
        }
    }
    return $out;    
} 

How to user this function ?

$pre = '{"status": 1,"message": "My Collection downloaded successfully.","myCollections":';     
$postd = ' }';  
$jsa_data = json_encode($res_arr); // pass your Array
echo $finalJson = json_pretty($pre.$jsa_data.$postd);

Out put

{
    "status": 1,
    "message": "All Post downloaded successfully",
    "postData": [
        {
            "id": 8,
            "name": "Cakes",
            "slug": "cakes",
            "parent": 0,
            "description": "Love is like a good cake; you never know when it's coming, but you'd better eat it when it does!",
            "count": 11
        },
        {
            "id": 9,
            "name": "Breads",
            "slug": "breads",
            "parent": 0,
            "description": "All sorrows are less with bread. ",
            "count": 3
        },
        {
            "id": 10,
            "name": "Pastries",
            "slug": "pastries",
            "parent": 0,
            "description": "I'm not a vegetarian! I'm a pastries-ian!",
            "count": 6
        }
    ]
}

Check it Json LINT http://jsonlint.com/

bhavesh vala
  • 873
  • 7
  • 14