|
|
|
อยากศึกษา วิธีรับค่า ใน JSON ด้วย PHP มีตัวอย่าง JSON DATA ด้านในครับ |
|
|
|
|
|
|
|
Code (PHP)
{
"auditData": {
"processTime": "94",
"timestamp": "2015-09-14 09:45:48.316",
"requestHost": "212.170.239.110",
"serverId": "ws3xmi01",
"environment": "[itest, iSET_A, ehcache]",
"release": "4838",
"token": "a7bbe92b-f9d7-480d-a65c-43070ef81b15",
"internal": "|02||2|5|null,null||||||||1"
},
"hotels": {
"hotels": [
{
"code": 2643,
"name": "Club Marthas",
"categoryCode": "4EST",
"categoryName": "4 STARS",
"destinationCode": "PMI",
"destinationName": "Majorca",
"zoneCode": 90,
"zoneName": "Cala d'Or",
"latitude": "39.364207",
"longitude": "3.226974",
"rooms": [
{
"code": "STU.C2",
"name": "STUDIO",
"rates": [
{
"rateKey": "2643|STU.C2|REP-OPACO|1|FB||1~1~1|8|20160608|20160610|W|N#236920111",
"rateClass": "NOR",
"rateType": "BOOKABLE",
"net": "158.020",
"allotment": 90,
"paymentType": "AT_WEB",
"packaging": false,
"boardCode": "FB",
"boardName": "FULL BOARD",
"cancellationPolicies": [
{
"amount": "79.010",
"from": "2016-06-08T00:00:00+02:00"
}
],
"rooms": 1,
"adults": 1,
"children": 1,
"childrenAges": "8",
"offers": [
{
"code": "9002",
"name": "Early Booking discount",
"amount": "-52.700"
}
]
}
],
"minRate": "86.320",
"maxRate": "119.060",
"currency": "EUR"
}
],
"checkIn": "2016-06-08",
"checkOut": "2016-06-10",
"total": 2
}
]
}
}
Response ได้ค่าดังกล่าว อยากรู้ว่าจะหยิบ JSON มาใช้อย่างไรดีครับ มีวิธีใดบ้างครับ ช่วยแนะนำหน่อยครับ
Tag : PHP
|
|
|
|
|
|
Date :
2017-09-07 11:45:31 |
By :
nut_ch31 |
View :
3452 |
Reply :
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่าง
Code (PHP)
$rs=json_decode($response);
echo $rs->auditData->processTime;
|
|
|
|
|
Date :
2017-09-07 11:57:17 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
$json = json_decode(file_get_contents('your.json'),true);
|
|
|
|
|
Date :
2017-09-07 11:58:00 |
By :
ดินทราย |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
$json = '
{
"auditData": {
"processTime": "94",
"timestamp": "2015-09-14 09:45:48.316",
"requestHost": "212.170.239.110",
"serverId": "ws3xmi01",
"environment": "[itest, iSET_A, ehcache]",
"release": "4838",
"token": "a7bbe92b-f9d7-480d-a65c-43070ef81b15",
"internal": "|02||2|5|null,null||||||||1"
},
"hotels": {
"hotels": [
{
"code": 2643,
"name": "Club Marthas",
"categoryCode": "4EST",
"categoryName": "4 STARS",
"destinationCode": "PMI",
"destinationName": "Majorca",
"zoneCode": 90,
"zoneName": "Cala d Or",
"latitude": "39.364207",
"longitude": "3.226974",
"rooms": [
{
"code": "STU.C2",
"name": "STUDIO",
"rates": [
{
"rateKey": "2643|STU.C2|REP-OPACO|1|FB||1~1~1|8|20160608|20160610|W|N#236920111",
"rateClass": "NOR",
"rateType": "BOOKABLE",
"net": "158.020",
"allotment": 90,
"paymentType": "AT_WEB",
"packaging": false,
"boardCode": "FB",
"boardName": "FULL BOARD",
"cancellationPolicies": [
{
"amount": "79.010",
"from": "2016-06-08T00:00:00+02:00"
}
],
"rooms": 1,
"adults": 1,
"children": 1,
"childrenAges": "8",
"offers": [
{
"code": "9002",
"name": "Early Booking discount",
"amount": "-52.700"
}
]
}
],
"minRate": "86.320",
"maxRate": "119.060",
"currency": "EUR"
}
],
"checkIn": "2016-06-08",
"checkOut": "2016-06-10",
"total": 2
}
]
}
}
';
$rs=json_decode($json);
echo $rs->auditData->timestamp;
?>
ถ้าผมต้องการเอาตัวแปร "amount": "79.010",
"from": "2016-06-08T00:00:00+02:00"
ออกมาต้องทำไงอ่ะครับ
|
ประวัติการแก้ไข 2017-09-07 12:06:42 2017-09-07 12:07:21
|
|
|
|
Date :
2017-09-07 12:06:13 |
By :
nut_ch31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันมีส่วนที่เป็น array ซ้อนอยู่ใน object
ส่วนที่เป็น array [] ก็อ้างแบบ array ส่วนที่เป็น object {} ก็อ้างแบบ object
Code (PHP)
echo $rs->hotels->hotels[0]->rooms[0]->rates[0]->cancellationPolicies[0]->amount;
echo $rs->hotels->hotels[0]->rooms[0]->rates[0]->cancellationPolicies[0]->from;
ปล.แก้ไขสกดผิด
|
ประวัติการแก้ไข 2017-09-07 12:27:58
|
|
|
|
Date :
2017-09-07 12:24:13 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
งั้นผมขอถามอีกหนอ่ยครับ ถ้าหาก ใน json ตามตัวอย่างด้านบน อ่ะ ใน hotel มันมี หลายๆ room แบบนี้ผมควรรับค่ามันอย่างไรดีเพื่อจะได้รู้ว่า Hotel นี้มีกี่ Room อ่ะครับ
แล้วในกรณีเดียวกันถ้ามันมีหลาย Hotel ผมจะรับค่าอย่างไรให้มันรู้ว่า มีกี่ Hotel อ่ะครับ
พอจะเข้าใจไหม๊เอ่ย
|
|
|
|
|
Date :
2017-09-07 14:11:42 |
By :
nut_ch31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
echo count($rs->hotels->hotels);
$hotel_index=0; // กำหนด hotels ที่ 1
echo count($rs->hotels->hotels[$hotel_index]->rooms);
|
|
|
|
|
Date :
2017-09-07 14:16:53 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
$json = '
{
"auditData": {
"processTime": "94",
"timestamp": "2015-09-14 09:45:48.316",
"requestHost": "212.170.239.110",
"serverId": "ws3xmi01",
"environment": "[itest, iSET_A, ehcache]",
"release": "4838",
"token": "a7bbe92b-f9d7-480d-a65c-43070ef81b15",
"internal": "|02||2|5|null,null||||||||1"
},
"hotels": {
"hotels": [
{
"code": 2643,
"name": "Club Marthas",
"categoryCode": "4EST",
"categoryName": "4 STARS",
"destinationCode": "PMI",
"destinationName": "Majorca",
"zoneCode": 90,
"zoneName": "Cala d Or",
"latitude": "39.364207",
"longitude": "3.226974",
"rooms": [
{
"code": "STU.C2",
"name": "STUDIO",
"rates": [
{
"rateKey": "2643|STU.C2|REP-OPACO|1|FB||1~1~1|8|20160608|20160610|W|N#236920111",
"rateClass": "NOR",
"rateType": "BOOKABLE",
"net": "158.020",
"allotment": 90,
"paymentType": "AT_WEB",
"packaging": false,
"boardCode": "FB",
"boardName": "FULL BOARD",
"cancellationPolicies": [
{
"amount": "79.010",
"from": "2016-06-08T00:00:00+02:00"
}
],
"rooms": 1,
"adults": 1,
"children": 1,
"childrenAges": "8",
"offers": [
{
"code": "9002",
"name": "Early Booking discount",
"amount": "-52.700"
}
]
}
],
"minRate": "86.320",
"maxRate": "119.060",
"currency": "EUR"
}
],
"checkIn": "2016-06-08",
"checkOut": "2016-06-10",
"total": 2
}
]
}
}
';
$rs=json_decode($json);
echo "processTime ==> ".$rs->auditData->processTime."<BR>";
echo "timestamp ==> ".$rs->auditData->timestamp."<BR>";
echo "requestHost ==> ".$rs->auditData->requestHost."<BR>";
echo "serverId ==> ".$rs->auditData->serverId."<BR>";
echo "environment ==> ".$rs->auditData->environment."<BR>";
echo "release ==> ".$rs->auditData->release."<BR>";
echo "token ==> ".$rs->auditData->token."<BR>";
echo "internal ==> ".$rs->auditData->internal."<BR>";
$count_hotel = count($rs->hotels->hotels);
for($x=0;$x<$count_hotel;$x++)
{
echo "code ==> ".$rs->hotels->hotels[$x]->code."<BR>";
echo "name ==> ".$rs->hotels->hotels[$x]->name."<BR>";
echo "categoryCode ==> ".$rs->hotels->hotels[$x]->categoryCode."<BR>";
echo "categoryName ==> ".$rs->hotels->hotels[$x]->categoryName."<BR>";
echo "destinationCode ==> ".$rs->hotels->hotels[$x]->destinationCode."<BR>";
echo "destinationName ==> ".$rs->hotels->hotels[$x]->destinationName."<BR>";
echo "zoneCode ==> ".$rs->hotels->hotels[$x]->zoneCode."<BR>";
echo "zoneName ==> ".$rs->hotels->hotels[$x]->zoneName."<BR>";
echo "latitude ==> ".$rs->hotels->hotels[$x]->latitude."<BR>";
echo "longitude ==> ".$rs->hotels->hotels[$x]->longitude."<BR>";
$count_room = count($rs->hotels->hotels[$x]->rooms);
for($y=0;$y<$count_room;$y++)
{
echo "code ==> ".$rs->hotels->hotels[$x]->rooms[$y]->code."<BR>";
echo "name ==> ".$rs->hotels->hotels[$x]->rooms[$y]->name."<BR>";
$count_rate = count($rs->hotels->hotels[$x]->rooms[$y]->rates);
for($z=0;$z<$count_rate;$z++)
{
echo "rateKey ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->rateKey."<BR>";
echo "rateClass ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->rateClass."<BR>";
echo "rateType ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->rateType."<BR>";
echo "net ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->net."<BR>";
echo "allotment ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->allotment."<BR>";
echo "paymentType ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->paymentType."<BR>";
echo "packaging ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->packaging."<BR>";
echo "boardCode ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->boardCode."<BR>";
echo "boardName ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->boardName."<BR>";
$count_cancellation = count($rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->cancellationPolicies);
for($c=0;$c<$count_cancellation;$c++)
{
echo "amount ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->cancellationPolicies[$c]->amount."<BR>";
echo "from ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->cancellationPolicies[$c]->from."<BR>";
}
echo "rooms ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->rooms."<BR>";
echo "adults ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->adults."<BR>";
echo "children ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->children."<BR>";
echo "childrenAges ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->childrenAges."<BR>";
$count_offers = count($rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->offers);
for($o=0;$o<$count_offers;$o++)
{
echo "code ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->offers[$o]->code."<BR>";
echo "name ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->offers[$o]->name."<BR>";
echo "amount ==> ".$rs->hotels->hotels[$x]->rooms[$y]->rates[$z]->offers[$o]->amount."<BR>";
}
}
echo "minRate ==> ".$rs->hotels->hotels[$x]->rooms[$y]->minRate."<BR>";
echo "maxRate ==> ".$rs->hotels->hotels[$x]->rooms[$y]->maxRate."<BR>";
echo "currency ==> ".$rs->hotels->hotels[$x]->rooms[$y]->currency."<BR>";
}
echo "checkIn ==> ".$rs->hotels->hotels[$x]->checkIn."<BR>";
echo "checkOut ==> ".$rs->hotels->hotels[$x]->checkOut."<BR>";
echo "total ==> ".$rs->hotels->hotels[$x]->total."<BR>";
}
?>
สุดยอดครับ คุณ Chaidhanan
จาก code get json ของผม ถ้าหาก json มันมี หลายโรงแรม แต่ละโรงแรมมีหลายห้อง จาก code ของผมน่าจะรับค่าได้อย่างถูกต้องไหม๊ครับ
ยังไงก็ขอบคุณมากครับ
|
|
|
|
|
Date :
2017-09-07 14:44:39 |
By :
nut_ch31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองศึกษาเรื่อง foreach จะเชียนโค๊ดได้สั้นและง่ายกว่า จะรับค่ามาเป็น object หรือ array
แล้วอ้างอิงจากค่าที่ได้นั้น ไม่ต้องมาเขียนโค๊ดยาวๆ
Code (PHP)
$rs=json_decode($json);
$audit=$rs->auditData;
echo "processTime ==> ".$audit->processTime."<BR>";
echo "timestamp ==> ".$audit->timestamp."<BR>";
echo "requestHost ==> ".$audit->requestHost."<BR>";
echo "serverId ==> ".$audit->serverId."<BR>";
echo "environment ==> ".$audit->environment."<BR>";
echo "release ==> ".$audit->release."<BR>";
echo "token ==> ".$audit->token."<BR>";
echo "internal ==> ".$audit->internal."<BR>";
$count_hotel = count($rs->hotels->hotels);
foreach($rs->hotels->hotels as $x=>$hotel){
echo "code ==> ".$hotel->code."<BR>";
echo "name ==> ".$hotel->name."<BR>";
echo "categoryCode ==> ".$hotel->categoryCode."<BR>";
echo "categoryName ==> ".$hotel->categoryName."<BR>";
echo "destinationCode ==> ".$hotel->destinationCode."<BR>";
echo "destinationName ==> ".$hotel->destinationName."<BR>";
echo "zoneCode ==> ".$hotel->zoneCode."<BR>";
echo "zoneName ==> ".$hotel->zoneName."<BR>";
echo "latitude ==> ".$hotel->latitude."<BR>";
echo "longitude ==> ".$hotel->longitude."<BR>";
$count_room = count($hotel->rooms);
foreach($hotel->rooms as $y=>$room){
echo "code ==> ".$room->code."<BR>";
echo "name ==> ".$room->name."<BR>";
$count_rate = count($room->rates);
foreach($room->rates as $z=>$rate){
echo "rateKey ==> ".$rate->rateKey."<BR>";
echo "rateClass ==> ".$rate->rateClass."<BR>";
echo "rateType ==> ".$rate->rateType."<BR>";
echo "net ==> ".$rate->net."<BR>";
echo "allotment ==> ".$rate->allotment."<BR>";
echo "paymentType ==> ".$rate->paymentType."<BR>";
echo "packaging ==> ".$rate->packaging."<BR>";
echo "boardCode ==> ".$rate->boardCode."<BR>";
echo "boardName ==> ".$rate->boardName."<BR>";
$count_cancellation = count($rate->cancellationPolicies);
foreach($rate->cancellationPolicies as $c=>$policies){
echo "amount ==> ".$policies->amount."<BR>";
echo "from ==> ".$policies->from."<BR>";
}
echo "rooms ==> ".$rate->rooms."<BR>";
echo "adults ==> ".$rate->adults."<BR>";
echo "children ==> ".$rate->children."<BR>";
echo "childrenAges ==> ".$rate->childrenAges."<BR>";
$count_offers = count($rate->offers);
foreach($rate->offers as $o=>$offer){
echo "code ==> ".$offer->code."<BR>";
echo "name ==> ".$offer->name."<BR>";
echo "amount ==> ".$offer->amount."<BR>";
}
}
echo "minRate ==> ".$room->minRate."<BR>";
echo "maxRate ==> ".$room->maxRate."<BR>";
echo "currency ==> ".$room->currency."<BR>";
}
echo "checkIn ==> ".$hotel->checkIn."<BR>";
echo "checkOut ==> ".$hotel->checkOut."<BR>";
echo "total ==> ".$hotel->total."<BR>";
}
|
|
|
|
|
Date :
2017-09-07 15:29:46 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับพี่ ที่สละเวลาให้ ขอบคุณครับ
|
|
|
|
|
Date :
2017-09-07 15:55:15 |
By :
nut_ch31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|