2016-10-06 21 views
1

Nachdem ich viele Stunden gesucht habe, schreibe ich diese Frage. Ich habe eine Variable namens $responses, die die codierten JSON-Objekte wie folgt ausgibt.Anzahl der JSON-Objekte zählen

[ 
{ 
    "notification_id": 4936, 
    "notification_title": "Bridge construction", 
    "notification_category": "Activities of extraterritorial organizations", 
    "notification_posted_date": "19/09/16", 
    "notification_time_left": "2016/10/11 12:45:00", 
    "notification_by_company": "The Media Company" 
} 
] 

Es gibt mehr als ein Objekte und ich versuche, die Nummer auch mit dem folgenden Code zu zählen.

echo json_encode($responses); 
echo count($responses); 

Aber es funktioniert nicht aus irgendeinem Grund. Ich versuchte es auch:

$JsonDecode = json_decode($responses, true);  
echo $JsonDecode; 

Die Hauptprobleme sind Drucken von JSON und Abrufen der Anzahl der Objekte. Jede Hilfe wird sehr geschätzt.

+2

Im zweiten Code haben Sie vergessen, 'count' – nospor

+0

können Sie ganze json bereitstellen? – Archish

+0

, die die komplette JSON-Antwort so weit ist, aber vom Kurs abgekommen Objekte es verringert werden kann und zugleich erhöht, was auf Wortgrenze i vollständige json Antwort keine Beiträge [ { „notification_id“: 4936, „notification_title ":" Brückenbau ", " notification_category ":" Aktivitäten von extraterritorialen Organisationen und Gremien ", " notification_posted_date ":" 19/09/16 ", " notification_time_left ":" 2016/10/11 12:45:00 ", " notification_by_company ":" The Media Company " } ] –

Antwort

-1

Wenn die $responses{...},{...}.... folgt dann könnte man versuchen

$explode = explode('},{', $responses); 
$count = count($explode); 
+0

Danke für Ihre schnelle Antwort, aber leider hat es nicht funktioniert $ explode = explode ('}, {' , $ Antworten); \t \t $ count = Anzahl ($ explode); \t \t \t \t echo $ zählen; es gibt 0 zurück, also denken Sie, dass es anders gelöst werden kann? –

+1

Bitte zeigen Sie uns einen größeren Teil des JSON – Blinkydamo

0

Das JSON-String stellt ein Array mit einer (vielleicht auch mehr) Objekt (e).

So die Eigenschaften eines jeden zu zählen Objekt, das Sie so etwas wie dieses

$s = '[ 
{ 
    "notification_id": 4936, 
    "notification_title": "Bridge construction", 
    "notification_category": "Activities of extraterritorial organizations", 
    "notification_posted_date": "19/09/16", 
    "notification_time_left": "2016/10/11 12:45:00", 
    "notification_by_company": "The Media Company" 
} 
]'; 

// Note using json_decode to convert a JSON string to its PHP equiv data type 
$j = json_decode($s); 

echo 'The number of objects in the array is ' . count($j) . '<br>'; 

foreach ($j as $inx => $obj) { 
    // count 
    echo "Object $inx has " . count((array)$obj) . ' Properties<br>'; 
} 

Ergebnis ist

The number of objects in the array is 1<br> 
Object 0 has 6 Properties<br> 

ich, dass ich es richtig verstanden hoffe tun konnte, was man wollte.