现在,我正在开展一个项目。我必须使用Woo-Commerce CLIENT REST API为Android应用程序构建一些完整的api。
一切正常,但我得到的数据是不必要的。所以有人能告诉我,如何克服这个问题。
e.g。我收到这个数据
{
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
}

但我不想要slug,parent,description参数。
提前致谢。
答案 0 :(得分:0)
密钥" "
和
中您的问题Ex: "ID", "name"
丢失了
您将此函数json_pretty()
用于json格式。
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;
}
如何使用此功能?
$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
}
]
}
检查Json LINT http://jsonlint.com/