Php 如何显示JSON结果中的单个结果

Php 如何显示JSON结果中的单个结果,php,json,curl,Php,Json,Curl,您好,我对php编码和使用curl非常陌生,所以回复时请记住这一点。我正试图从一个JSON对象中获取一个元素,但却不知道如何实现。到目前为止,我已经写了这篇文章,虽然不多,但似乎无法让它工作。如果您能提供任何帮助,我将不胜感激 $curl=curl_init("https://api.brawlhalla.com/legend/3/?api_key=KEY"); $resp=curl_exec ($curl); $jsonObj=json_decode($resp); echo $jsonOb

您好,我对php编码和使用curl非常陌生,所以回复时请记住这一点。我正试图从一个JSON对象中获取一个元素,但却不知道如何实现。到目前为止,我已经写了这篇文章,虽然不多,但似乎无法让它工作。如果您能提供任何帮助,我将不胜感激

$curl=curl_init("https://api.brawlhalla.com/legend/3/?api_key=KEY");
$resp=curl_exec ($curl); 
$jsonObj=json_decode($resp);
echo $jsonObj->legend_id;
curl_close($curl);
这就是json对象的外观

{ "legend_id": 3, "legend_name_key": "bodvar", "bio_name": "B\u00f6dvar", "bio_aka": "The Unconquered Viking, The Great Bear", "bio_quote": "\u201cI speak, you noble vikings, of a warrior who surpassed you all. I tell of a great bear-man who overcame giants and armies, and of how he came to leave our world and challenge the Gods.\u201d", "bio_quote_about_attrib": "\"-The Saga of B\u00f6dvar Bearson, first stanza\"", "bio_quote_from": "\"Listen you nine-mothered bridge troll, I'm coming in, and the first beer I'm drinking is the one in your fist.\"", "bio_quote_from_attrib": "\"-B\u00f6dvar to Heimdall, guardian of the gates of Asgard\"", "bio_text": "Born of a viking mother and bear father, B\u00f6dvar grew up feared and mistrusted by his own people.\nB\u00f6dvar's first nemesis was the terrible giant bear Grothnar, his own brother. By defeating Grothnar in a battle that lasted seven days, B\u00f6dvar chose to side with humanity and became the protector of the people of the north. He led his Skandian people against the Witch Queen of Helheim, slew the White Dragon Sorcerer, and lived the life of an all-conquering hero.\nAfter he single-handedly ended the Giant Wars by trapping the fire giant king in his own volcano, B\u00f6dvar sensed his work was done. But he felt doomed to never be taken by the Valkyries to Valhalla because he could never manage to be defeated in battle. So he travelled to Asgard himself, broke down the doors, and let himself in.\nValhalla is everything B\u00f6dvar hoped - an endless reward of feasting and fighting, with himself among its greatest champions.", "bot_name": "B\u00f6tvar", "weapon_one": "Hammer", "weapon_two": "Sword", "strength": "6", "dexterity": "6", "defense": "5", "speed": "5" }

缺少控制打印响应或将其返回到变量的选项。在curl_init下面使用以下代码:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
CURLOPT_RETURNTRANSFER-如果为TRUE,则将传输作为curl_exec()返回值的字符串返回,而不是直接输出


阅读更多关于它的信息

您需要了解json_解码。获取一个数组。这应该会有所帮助。