【问题标题】:Add data to JSON retrieved from URL, PHP将数据添加到从 URL、PHP 检索到的 JSON
【发布时间】:2017-11-05 00:04:27
【问题描述】:

我正在使用 Wikipedia 网络服务来获取结果。我喜欢在 PHP 中向检索到的 JSON 文件添加更多数据。我知道您需要在添加更多数据之前对 JSON 文件进行解码,但没有显示如何按以下格式添加数据;

我的 PHP 代码:

<?php

//---------------------Wikipedia Web Service-------------------
$json_array = array();
$answer_data = array();

$command_text = "";

if(isset($_POST["command"]) && !empty($_POST["command"])){
    $command_text = $_POST["command"];
}

//JSON link and get data and then send data back to client
$url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=' . $command_text .'&format=json';
$json = file_get_contents($url);
$json_decode = json_decode($json, true);



$newer['query']['search'][20] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!");

array_push($json_decode, $newer);

//----------------OUTPUT JSON-----------------
//header function converts JSON to objects (no need JSON.parse() javascript function)!
header("Content-type:application/json");
echo json_encode($json_decode);

?>

JSON 文件:

{"batchcomplete":"","continue":{"sroffset":10,"continue":"-||"},"query":{"searchinfo":{"totalhits":20894},"search":[{"ns":0,"title":"Owl","pageid":37654,"size":52826,"wordcount":6200,"snippet":"<span class=\"searchmatch\">Owls</span> are birds from the order Strigiformes, which includes about 200 species of mostly solitary and nocturnal birds of prey typified by an upright stance","timestamp":"2017-11-02T10:44:00Z"},{"ns":0,"title":"Owl (disambiguation)","pageid":4577780,"size":6478,"wordcount":843,"snippet":"<span class=\"searchmatch\">Owls</span> are nocturnal birds of prey. <span class=\"searchmatch\">Owl</span>, <span class=\"searchmatch\">Owls</span> or <span class=\"searchmatch\">OWL</span> may also refer to:   Fraternal Order of <span class=\"searchmatch\">Owls</span>, a fraternal order of the United States <span class=\"searchmatch\">Owl</span> Club (Harvard)","timestamp":"2017-10-25T23:41:30Z"},{"ns":0,"title":"Great grey owl","pageid":358689,"size":20254,"wordcount":2757,"snippet":"The great grey <span class=\"searchmatch\">owl</span> or great gray <span class=\"searchmatch\">owl</span> (Strix nebulosa) is a very large <span class=\"searchmatch\">owl</span>, documented as the world's largest species of <span class=\"searchmatch\">owl</span> by length. It is distributed","timestamp":"2017-10-23T04:19:32Z"},{"ns":0,"title":"Snowy owl","pageid":252110,"size":17438,"wordcount":1969,"snippet":"The snowy <span class=\"searchmatch\">owl</span> (Bubo scandiacus) is a large, white <span class=\"searchmatch\">owl</span> of the typical <span class=\"searchmatch\">owl</span> family. Snowy <span class=\"searchmatch\">owls</span> are native to Arctic regions in North America and Eurasia","timestamp":"2017-10-31T18:05:56Z"},{"ns":0,"title":"Web Ontology Language","pageid":248001,"size":41757,"wordcount":4380,"snippet":"The Web Ontology Language (<span class=\"searchmatch\">OWL</span>) is a family of knowledge representation languages for authoring ontologies. Ontologies are a formal way to describe taxonomies","timestamp":"2017-09-27T21:44:22Z"},{"ns":0,"title":"Barn owl","pageid":244511,"size":70869,"wordcount":7941,"snippet":"flight: white <span class=\"searchmatch\">owl</span>, silver <span class=\"searchmatch\">owl</span>, demon <span class=\"searchmatch\">owl</span>, ghost <span class=\"searchmatch\">owl</span>, death <span class=\"searchmatch\">owl</span>, night <span class=\"searchmatch\">owl</span>, rat <span class=\"searchmatch\">owl</span>, church <span class=\"searchmatch\">owl</span>, cave <span class=\"searchmatch\">owl</span>, stone <span class=\"searchmatch\">owl</span>, monkey-faced <span class=\"searchmatch\">owl</span>, hissing <span class=\"searchmatch\">owl</span>, hobgoblin","timestamp":"2017-11-04T01:43:51Z"},{"ns":0,"title":"Great horned owl","pageid":358093,"size":142193,"wordcount":19716,"snippet":"The great horned <span class=\"searchmatch\">owl</span> (Bubo virginianus), also known as the tiger <span class=\"searchmatch\">owl</span> (originally derived from early naturalists' description as the &quot;winged tiger&quot; or","timestamp":"2017-11-01T07:00:24Z"},{"ns":0,"title":"Owl City","pageid":23585683,"size":56754,"wordcount":5700,"snippet":"<span class=\"searchmatch\">Owl</span> City is an American electronica project created in 2007 in Owatonna, Minnesota; it is one of several projects by singer, songwriter and multi-instrumentalist","timestamp":"2017-11-04T18:48:37Z"},{"ns":0,"title":"Eastern screech owl","pageid":361206,"size":36661,"wordcount":4715,"snippet":"The eastern screech <span class=\"searchmatch\">owl</span> or eastern screech-<span class=\"searchmatch\">owl</span> (Megascops asio) is a small <span class=\"searchmatch\">owl</span> that is relatively common in Eastern North America, from Mexico to Canada","timestamp":"2017-10-22T20:26:20Z"},{"ns":0,"title":"Owl of Athena","pageid":3206672,"size":11552,"wordcount":1045,"snippet":"In Greek mythology, a little <span class=\"searchmatch\">owl</span> (Athene noctua) traditionally represents or accompanies Athena, the virgin goddess of wisdom, or Minerva, her syncretic","timestamp":"2017-10-06T10:34:30Z"}]}}

我喜欢在搜索标签 (JSON) 中添加我的新数据,如何使用 PHP 在孙子中添加新数据?

亲切的问候

【问题讨论】:

  • 尝试删除 json 文件中所有以前的数据?

标签: javascript php arrays json web-services


【解决方案1】:

而不是这个:

$newer['query']['search'][20] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!");

你可以这样做:

$json_decode['query']['search'][20]['title'] = 'OWL Witch';
$json_decode['query']['search'][20]['snippet'] = 'One crazy owl!';

你真的不应该调用你的变量 $json_decode ,这很令人困惑

编辑

我刚刚查看了您的 json,并意识到这可能不是最好的方法。我以为您想将特定信息添加到 $json_decode['query']['search'][20]

如果您真正想做的是在现有条目的末尾添加一个新条目,那么更好的方法是:

$json_decode['query']['search'][] = array("title"=>"OWL Witch", "snippet"=>"One crazy owl!");

这会将您的新条目添加为 $json_decode['query']['search'] 数组中的下一个数字数组键,无论数组中当前有多少条目。

【讨论】:

  • 为结果干杯。
  • 不客气。我已经更新了我的答案,因为在查看了你的 json 之后,我意识到我原来的答案可能并没有完全按照你想要做的那样做。
猜你喜欢
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多