【问题标题】:How to remove the unwanted nested keys from JSON如何从 JSON 中删除不需要的嵌套键
【发布时间】:2017-09-27 08:42:22
【问题描述】:

这是我的 json:

{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}

我想使用 PHP 删除二级密钥(例如 "26:""28":)。

换句话说,我想用零索引数字键替换双引号数字键。

我怎样才能让它看起来像这样:

{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}

【问题讨论】:

  • 如果你真的只是删除了它们,你就会得到无效的 JSON,这可能是你不想要的。你想要什么结果?另外:不清楚您在问什么:您是在尝试使用 PHP 代码来解析和转换此 JSON,还是从现有 PHP 代码中获取此结果并希望更改该代码以产生不同的结果?
  • 这感觉就像XY problem。你真正想做什么?
  • ...当然...你自己试过什么?
  • 是的,我想将我的 json 传递到 android 应用程序中,由于该值,这个值不会发布到 android。@T.J.Crowder
  • 现在是时候更新您的问题,以便为到目前为止的所有 cmets 提供答复。如果不这样做,答案将基于假设。

标签: php json reindex array-key


【解决方案1】:

这里是demo

代码:

// declare $json
$array=json_decode($json,true);  // decode as array
// overwrite subarray with zero-indexed keys while preserving subarray values
$array['all_counts_reports']=array_values($array['all_counts_reports']);
var_export(json_encode($array));  // return to json

输入:

$json='{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}';

输出:

'{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}'

在您的 javascript 中,使用 JSON.parse() 去除包装单引号:

var str='{"all_counts_reports":[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]}';
var json=JSON.parse(str);
console.log(json);

由于我们是在假设条件下运行,如果您也想删除 all_counts_reports 键,您可以使用这个单行:

代码:

$new_json=json_encode(array_values(current(json_decode($json,true))));

输出:

'[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]'

【讨论】:

  • 它的工作,但在开始和结束有一个单引号。我用$result = str_replace('"', '', json_encode($array));这个来解决这个问题
  • @teddycherrykj 我对您的问题进行了大量编辑。如果有任何内容歪曲您的问题,请随时再次编辑问题,使其正确无误。
【解决方案2】:
$array = json_decode($your_json_string,true);
print_r($array);
$result = array();
foreach($array['all_counts_reports'] as $rep){
    $result['all_counts_reports'][] = array(
        "name"=>$rep['name'],
        "date"=>$rep['date'],
        "trips_per_day"=>$rep['trips_per_day'],
        "cash_trips"=>$rep['cash_trips'],
        "credit_trips"=>$rep['credit_trips'],
        "compliment_trips"=>$rep['compliment_trips'], 
    );
}

$result_json = json_encode($result);
echo $result_json;

可能有更好的解决方案,但我现在想到了这个

不清楚您要查找的内容,如果您只想删除并且不想保留为原始 json,那么您可以像这个示例一样进行操作。但是,如果您不希望将您的 all_counts_reports 视为数组 [],那么此示例将无济于事。

然后简单地传递给android然后上面就可以了。

【讨论】:

    猜你喜欢
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2017-03-08
    相关资源
    最近更新 更多