-1

I have a problem echo'ing data from a JSON file:

<?php
$url = file_get_contents("http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80");
$json = file_get_contents($url);
$data = json_decode($json, true);
echo "<pre>"; var_dump($data); echo"</pre>";
?>

the above is my php file i am using which outputs the JSON as shown below:

{

    "id": 3121752,
    "name": "SnowderBlazer",
    "birth": "2010-04-10",
    "avatar": "http://static.erepublik.net/uploads/avatars/Citizens/2010/04/10/4bb9a72cc291faaaf7af8e78ed0a8509_100x100.jpg",
    "online": false,
    "ban": null,
    "alive": true,
    "level": 97,
    "experience": 360391,
    "strength": 42859.62,
    "rank": {
        "points": 437120237,
        "level": 64,
        "image": "http://www.erepublik.com/images/modules/ranks/god_of_war_2.png",
        "name": "God of War**"
    },
    "elite_citizen": false,
    "national_rank": 1,
    "residence": {
        "country": {
            "id": 65,
            "name": "Serbia",
            "code": "RS"
        },
        "region": {
            "id": 198,
            "name": "Midi-Pyrenees"
        }
    },
    "citizenship": {
        "id": 65,
        "name": "Serbia",
        "code": "RS"
    },
    "about": "Voters Club Moderator\ncatch me on #voters @Rizon\nIRC Nick : Snowderblazer OR Snowderblazer[BNC]\norder herehttp://erepublik-market.com/voters/newOrder.html?adp=1549866\n[ident:9vrwQZB9]",
    "party": {
        "id": 2479,
        "name": "Ujedinjena eSrbija",
        "role": "Party Member"
    },
    "army": {
        "id": 1980,
        "name": "Legija Stranaca Elite",
        "role": "Commander",
        "created_at": "2012-05-26",
        "avatar": "http://static.erepublik.net/uploads/avatars/Groups/2012/05/26/f80bf05527157a8c2a7bb63b22f49aaa_medium.jpg",
        "rank": "Commander"
    },
    "newspaper": {
        "id": 241367,
        "role": "Press director",
        "name": "M.A.D.S News"
    },
    "top_damage": {
        "damage": 215238312,
        "date": "2013-05-16",
        "message": "Achieved while successfully defending Basilicata against Italy on day 2,004"
    },
    "true_patriot": {
        "damage": 3021790429,
        "since": "2012-04-26"
    },
    "medals": {
        "battle_hero": 248,
        "campaign_hero": 98,
        "congress_member": 9,
        "country_president": 0,
        "hard_worker": 36,
        "media_mogul": 5,
        "mercenary": 1,
        "resistance_hero": 2,
        "society_builder": 0,
        "super_soldier": 171,
        "top_fighter": 3,
        "true_patriot": 43
    },
    "hit": 14924

}

my problem is just that it loads all data at once, and i only want to echo each i have no experience with JSON so i don't knwo the variables to echo

the via the JSON you can visit

JSON Version : http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80
XML Version : http://api.erpk.org/citizen/profile/3121752.xml?key=Yn3AsG80

What I want to achieve is the following:

  1. Load the JSON or XML without the output so that I can use the ECHO to put he data where i want it to show in the PHP file;
  2. ECHO each of the JSON or XML data.

Everytime I try I get an error that says its a non-object

4

2 回答 2

1

json_decode($json, true)返回一个普通数组,它不是特定于 json 的,您可以像访问其他数组一样访问它的元素:

$data = json_decode($json, true);
var_dump($data['name']);

如果您想获取对象,请删除第二个参数:

$data = json_decode($json);
var_dump($data->name);
于 2013-05-27T18:56:43.207 回答
0

你在滥用file_get_contents,所以我猜这是你的问题。要从该 URL 获取数据,只需使用该 URL 的第一行:

$url = 'http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80';

无论如何,这是一个代码片段,可以根据您的 JSON 数据集回显一个简短的配置文件。

它向您展示了如何访问 1 级字段,如nameand birth,2 级字段rank\levelrank\name最后是一组动态字段,如medals.

<?php
$json = '{"id":3121752,"name":"SnowderBlazer","birth":"2010-04-10","avatar":"http://static.erepublik.net/uploads/avatars/Citizens/2010/04/10/4bb9a72cc291faaaf7af8e78ed0a8509_100x100.jpg","online":false,"ban":null,"alive":true,"level":97,"experience":360391,"strength":42859.62,"rank":{"points":437120237,"level":64,"image":"http://www.erepublik.com/images/modules/ranks/god_of_war_2.png","name":"God of War**"},"elite_citizen":false,"national_rank":1,"residence":{"country":{"id":65,"name":"Serbia","code":"RS"},"region":{"id":198,"name":"Midi-Pyrenees"}},"citizenship":{"id":65,"name":"Serbia","code":"RS"},"about":"Voters Club Moderator\ncatch me on #voters @Rizon\nIRC Nick : Snowderblazer OR Snowderblazer[BNC]\norder herehttp://erepublik-market.com/voters/newOrder.html?adp=1549866\n[ident:9vrwQZB9]","party":{"id":2479,"name":"Ujedinjena eSrbija","role":"Party Member"},"army":{"id":1980,"name":"Legija Stranaca Elite","role":"Commander","created_at":"2012-05-26","avatar":"http://static.erepublik.net/uploads/avatars/Groups/2012/05/26/f80bf05527157a8c2a7bb63b22f49aaa_medium.jpg","rank":"Commander"},"newspaper":{"id":241367,"role":"Press director","name":"M.A.D.S News"},"top_damage":{"damage":215238312,"date":"2013-05-16","message":"Achieved while successfully defending Basilicata against Italy on day 2,004"},"true_patriot":{"damage":3021790429,"since":"2012-04-26"},"medals":{"battle_hero":248,"campaign_hero":98,"congress_member":9,"country_president":0,"hard_worker":36,"media_mogul":5,"mercenary":1,"resistance_hero":2,"society_builder":0,"super_soldier":171,"top_fighter":3,"true_patriot":43},"hit":14924}';

$data = json_decode($json, true);

echo "Name: {$data['name']}\n";
echo "Date of birth: {$data['birth']}\n\n";

echo "Level: {$data['rank']['level']}\n";
echo "Rank: {$data['rank']['name']}\n\n";

echo "Medals\n";
foreach ($data['medals'] as $medalName => $number) {
    echo ucfirst(str_replace('_', ' ', $medalName)) . ": {$number}\n";
}

输出

Name: SnowderBlazer
Date of birth: 2010-04-10

Level: 64
Rank: God of War**

Medals
Battle hero: 248
Campaign hero: 98
Congress member: 9
Country president: 0
Hard worker: 36
Media mogul: 5
Mercenary: 1
Resistance hero: 2
Society builder: 0
Super soldier: 171
Top fighter: 3
True patriot: 43
于 2013-05-27T19:44:39.077 回答