我当前的JSON值是这样的:
{"vegetable_names":["vegetables 1","vegetables 2","vegetables 3"]}{"grade":["XXL","A","B","S"]}{"packages":["Carton Boxes","Baskets"]}
但我想要输出如下:
{"vegetable_names":["vegetables 1","vegetables 2","vegetables 3"],"grade":["XXL","A","B","S"],"packages":["Carton Boxes","Baskets"]}
答案 0 :(得分:0)
我想你会像你描述的那样得到Json。如果不是,我建议你从一开始就遵循Paul Crovella的解决方案。
所以假设你没有选择,如何将Json解码为合并这两个或更多的对象或数组,然后再将它们编码为Json。 很丑,但是如果Json是由独立于你的源创建的,你可以这样做。
答案 1 :(得分:0)
JSON
只是某些数据结构的文本表示。从JSON字符串恢复数据结构,使用PHP对其进行修改(或者如果它更合适,则创建新的数据结构),再次将更新的/新数据结构编码为JSON
。
似乎" JSON"您发布的输入不是JSON
。它是三个JSON
字符串的串联。它不会这样工作!
// Input JSON strings
$json1 = '{"vegetable_names":["vegetables 1","vegetables 2","vegetables 3"]}';
$json2 = '{"grade":["XXL","A","B","S"]}';
$json3 = '{"packages":["Carton Boxes","Baskets"]}';
// Restore the data structures as arrays
$data1 = json_decode($json1, TRUE);
$data2 = json_decode($json2, TRUE);
$data3 = json_decode($json3, TRUE);
// Combine them; it seems all you need is a simple merging
$data = array_merge($data1, $data2, $data3);
// Encode the combined arrays as JSON again
$output = json_encode($data);