【问题标题】:trouble to update or edit the json file using php使用 php 更新或编辑 json 文件的麻烦
【发布时间】:2013-03-30 08:16:10
【问题描述】:

index.json

myjsoncallback([{"ProductID":1,"ProductName":"Chai","UnitPrice":18,"UnitsInStock":39,"Discontinued":false}])

PHP 代码:

$string = file_get_contents("../index.json");

$string = utf8_encode($string);

$json_data = json_decode($string, true);

echo $json_data;

$i=count($json_data);

$json_data[$i] = array('ProductID'=>$fileName,'ProductName'=>$name,'ptext'=>$message,'Discontinued'=>false);

$fp = fopen('../index.json', 'a');

fwrite($fp, json_encode($json_data));

fclose($fp);

我想用 jsonpcallback 将数据写入 jsone 文件。

【问题讨论】:

  • 为什么要使用 utf8_encode()

标签: php javascript json jsonp jquery-callback


【解决方案1】:

如果您的文件包含 JSONP 数据,则不能使用 json_decode()。如果你想用 PHP 处理 JSON 数据,你必须移除 JSONP 的回调表单,只对 JSON 对象进行编码/解码。

【讨论】:

  • 那是一个不同的问题。您仍然不能将 json_encode()json_decode() 与 JSONP 数据一起使用。您必须删除回调以使用 PHP 处理此类数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 2019-06-14
  • 1970-01-01
  • 2013-06-15
相关资源
最近更新 更多