2016-09-13 5 views
1

Ниже приводится выход JSONИзвлечение информации из строки JSON из PHP

{ 
    "bugs": [ 
{ 
    "is_confirmed": true, 
    "cc": [ 

    ], 
    "deadline": null, 
    "qa_contact": "", 
    "last_change_time": "2016-09-12T04:57:53Z", 
    "creation_time": "2016-09-12T04:57:03Z", 
    "keywords": [ 

    ], 
    "classification": "Unclassified", 
    "assigned_to": "[email protected]", 
    "priority": "Highest", 
    "is_open": true, 
    "creator": "[email protected]", 
    "target_milestone": "---", 
    "creator_detail": { 
    "name": "[email protected]", 
    "id": 1, 
    "real_name": "Ashish Sureka", 
    "email": "[email protected]" 
    }, 
    "is_cc_accessible": true, 
    "op_sys": "Windows", 
    "component": "TestComponent", 
    "status": "CONFIRMED", 
    "resolution": "", 
    "platform": "PC", 
    "depends_on": [ 

    ], 
    "groups": [ 

    ], 
    "summary": "Checking if Bugzilla REST API is working or not", 
    "cc_detail": [ 

    ], 
    "assigned_to_detail": { 
    "name": "[email protected]", 
    "email": "[email protected]", 
    "id": 1, 
    "real_name": "Ashish Sureka" 
    }, 
    "alias": [ 

    ], 
    "whiteboard": "", 
    "version": "unspecified", 
    "url": "", 
    "see_also": [ 

    ], 
    "id": 1, 
    "dupe_of": null, 
    "severity": "critical", 
    "product": "TestProduct", 
    "blocks": [ 

    ], 
    "is_creator_accessible": true, 
    "flags": [ 

    ] 
} 
], 
"faults": [ 

] 
} 

моей программы

 // Location of the resource (Bug Report) 
     $url = "http://localhost:8080/bugzilla/rest/bug/1"; 

     // Instantiate an instance of cURL 
     $curl = curl_init(); 

     // cURL settings, URL to send request to 
     curl_setopt($curl, CURLOPT_URL, $url); 

     // cURL settings, Return the response as a string 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

     // Execute the cURL request 
     $result = curl_exec($curl); 

     // Close the cURL request 
     curl_close($curl); 

     //echo "<BR>Product: ". $result->product; 
     //echo "<BR>Component: ".$result->component; 
     //echo "<BR>Version: ".$result->version; 
     //echo "<BR>Summary: ".$result->summary; 
     //echo "<BR>Priority: ".$result->priority; 
     //echo "<BR>Operating System".$result->op_sys; 

Я попытался с помощью json_encode и json_decode функции и попытался извлечь свойства из объекта или получить информацию из массива, но не может получить продукт, компонент, версию, сводку, приоритет и т. д. от $result

+1

Возможный дубликат [? Как извлечь данные из JSON с PHP] (http://stackoverflow.com/questions/ 29308898/how-do-i-extract-data-from-json-with-php) –

ответ

2

T чень это

$data = json_decode('your json_code',true); 
echo $data['bugs'][0]['component']."<br>"; 
echo $data['bugs'][0]['product']."<br>"; 
echo $data['bugs'][0]['version']."<br>"; 
echo $data['bugs'][0]['summary']."<br>"; 

Выход

TestComponent 
TestProduct 
unspecified 
Checking if Bugzilla REST API is working or not 

В json_decode Когда TRUE вернулся objects будут преобразованы в associative arrays. http://php.net/manual/en/function.json-decode.php

+0

Спасибо за вашу помощь, и я очень ценю это. Ваше решение сработало! - – user1468768

+0

Ваше приветствие Спасибо, что приняли его :) – user1234

1

Попробуйте это:

<?php 

$str = '{ 
    "bugs":[ 
     { 
     "is_confirmed":true, 
     "cc":[ 

     ], 
     "deadline":null, 
     "qa_contact":"", 
     "last_change_time":"2016-09-12T04:57:53Z", 
     "creation_time":"2016-09-12T04:57:03Z", 
     "keywords":[ 

     ], 
     "classification":"Unclassified", 
     "assigned_to":"[email protected]", 
     "priority":"Highest", 
     "is_open":true, 
     "creator":"[email protected]", 
     "target_milestone":"---", 
     "creator_detail":{ 
      "name":"[email protected]", 
      "id":1, 
      "real_name":"Ashish Sureka", 
      "email":"[email protected]" 
     }, 
     "is_cc_accessible":true, 
     "op_sys":"Windows", 
     "component":"TestComponent", 
     "status":"CONFIRMED", 
     "resolution":"", 
     "platform":"PC", 
     "depends_on":[ 

     ], 
     "groups":[ 

     ], 
     "summary":"Checking if Bugzilla REST API is working or not", 
     "cc_detail":[ 

     ], 
     "assigned_to_detail":{ 
      "name":"[email protected]", 
      "email":"[email protected]", 
      "id":1, 
      "real_name":"Ashish Sureka" 
     }, 
     "alias":[ 

     ], 
     "whiteboard":"", 
     "version":"unspecified", 
     "url":"", 
     "see_also":[ 

     ], 
     "id":1, 
     "dupe_of":null, 
     "severity":"critical", 
     "product":"TestProduct", 
     "blocks":[ 

     ], 
     "is_creator_accessible":true, 
     "flags":[ 

     ] 
     } 
    ], 
    "faults":[ 

    ] 
}'; 

$result = json_decode($str); 

echo $result->bugs[0]->product; 
echo PHP_EOL; 
echo $result->bugs[0]->component; 
echo PHP_EOL; 
echo $result->bugs[0]->version; 

Выход:

TestProduct 
TestComponent 
unspecified 
+0

Спасибо за вашу помощь, и я очень ценю это. Ваше решение сработало! – user1468768

1

Я думаю, вы должны смотреть на структуру JSON. Это действительный JSON, поэтому вы можете использовать функцию PHP json_decode ($ result) для анализа JSON в объект.

Затем используйте var_dump(), чтобы увидеть четкий обзор объекта. Там вы можете увидеть, как вы можете получить информацию.

Пример:

$result = json_decode($result); 
var_dump($result); 

//Product: 
$result->bugs[0]->product; 

//Component: 
$result->bugs[0]->component; 

JSON валидатор:http://jsonlint.com/

+0

Спасибо за вашу помощь, и я очень ценю это. Это сработало ! – user1468768

Смежные вопросы