如何从url获取php中的json输出并使用foreach获取特定结果

时间:2018-03-03 04:02:28

标签: php

首先,我真的不知道如何获得json的结果,这样的结果非常复杂:

{
  "SearchTime": 190,
  "TotalTime": 190,
  "SuggestedResponseType": "Person",
  "ResultList": [
    {
      "__type": "ContactResult",
      "TotalCount": 57079,
      "Type": "Person",
      "Contacts": [
        {
          "UnitId": 459551,
          "Id": "459551S1",
          "Name": "John d",
          "VisitationAddress": "Lauvha 7, 87820",
          "PostAddress": null,
          "Value": "74 00 00 00",
          "ValueType": "Phone",
          "Distance": null,
          "Rank": 0,
          "BusinessName": null,
          "Coordinate": {
            "Lat": 64.4595741,
            "Lon": 11.55081
          },
          "Logo": null,
          "ContactPoints": [
            {
              "DisplayValue": "911 00 000",
              "Indent": 2,
              "Label": "Mobiltelefon",
              "MainInfo": false,
              "Type": "MobilePhone",
              "Value": "91000000"
            }
          ],
          "FirstName": null,
          "LastName": null,
          "OrganizationNumber": null,
          "Address": {
            "PostAddress": null,
            "StreetAddress": {
              "CityArea": null,
              "Coordinate": {
                "Lat": 94.4595741,
                "Lon": 71.55081
              },
              "County": "Tr\u00c3\u00b8ndelag",
              "DisplayValue": "Lauvha 7 87820 Spilm",
              "Entrance": "",
              "HouseNumber": "7",
              "Houses": null,
              "Municipal": "Namsos",
              "PostArea": "Spilm",
              "PostCode": "87820",
              "Street": "Lauvha"
            }
          },
          "Products": null,
          "ProfilePictures": null,
          "ContactType": "Person"
        },
        {
          "UnitId": 0,
          "Id": "Footer",
          "Name": "Vis flere treff",
          "VisitationAddress": null,
          "PostAddress": null,
          "Value": "{\"q\":\"john\",\"type\":\"p\",\"start\":1,\"page\":2}",
          "ValueType": null,
          "Distance": null,
          "Rank": 0,
          "BusinessName": null,
          "Coordinate": null,
          "Logo": null,
          "ContactPoints": null,
          "FirstName": null,
          "LastName": null,
          "OrganizationNumber": null,
          "Address": null,
          "Products": null,
          "ProfilePictures": null,
          "ContactType": null
        }
      ]
    }
  ],
  "Stunts": null
}

下一步:如何使用foreach获取输出:name,VisitationAddress和来自(ContactPoints)的值为正确格式

并摆脱

"Name": "Vis flere treff",    
"Value": "{\"q\":\"john\",\"type\":\"p\",\"start\":1,\"page\":2}",

在最后一行。

1 个答案:

答案 0 :(得分:0)

您需要了解如何在PHP中与对象和数组进行交互。

$object->property$array['key']$object->array_property[$index]->key

对于您的特定情况,您需要使用cURL或file_get_contents()或您想要的任何其他方法来获取JSON。然后,您需要使用json_decode()解码响应。

获得JSON对象之后,只需检查是否存在值并根据需要输出它们。请注意,对象内部有一堆数组,但以下内容将启动

<?php

$response = file_get_contents( 'https://zabihullah.com/veg/example.json' );
//$response = '{"SearchTime":190,"TotalTime":190,"SuggestedResponseType":"Person","ResultList":[{"__type":"ContactResult","TotalCount":57079,"Type":"Person","Contacts":[{"UnitId":459551,"Id":"459551S1","Name":"John d","VisitationAddress":"Lauvha 7, 87820","PostAddress":null,"Value":"74 00 00 00","ValueType":"Phone","Distance":null,"Rank":0,"BusinessName":null,"Coordinate":{"Lat":64.4595741,"Lon":11.55081},"Logo":null,"ContactPoints":[{"DisplayValue":"911 00 000","Indent":2,"Label":"Mobiltelefon","MainInfo":false,"Type":"MobilePhone","Value":"91000000"}],"FirstName":null,"LastName":null,"OrganizationNumber":null,"Address":{"PostAddress":null,"StreetAddress":{"CityArea":null,"Coordinate":{"Lat":94.4595741,"Lon":71.55081},"County":"Trøndelag","DisplayValue":"Lauvha 7 87820 Spilm","Entrance":"","HouseNumber":"7","Houses":null,"Municipal":"Namsos","PostArea":"Spilm","PostCode":"87820","Street":"Lauvha"}},"Products":null,"ProfilePictures":null,"ContactType":"Person"},{"UnitId":0,"Id":"Footer","Name":"Vis flere treff","VisitationAddress":null,"PostAddress":null,"Value":"{\"q\":\"john\",\"type\":\"p\",\"start\":1,\"page\":2}","ValueType":null,"Distance":null,"Rank":0,"BusinessName":null,"Coordinate":null,"Logo":null,"ContactPoints":null,"FirstName":null,"LastName":null,"OrganizationNumber":null,"Address":null,"Products":null,"ProfilePictures":null,"ContactType":null}]}],"Stunts":null}';

$json = json_decode( $response );

foreach( $json->ResultList[0]->Contacts as $contact ){
    //var_dump( $contact );

    if( $contact->Name == 'Vis flere treff' ) continue;

    if( !empty( $contact->Name ) ) echo "Name: {$contact->Name}\r\n";
    if( !empty( $contact->VisitationAddress ) ) echo "Address: {$contact->VisitationAddress}\r\n";
    if( !empty( $contact->Value ) ) echo "Value: ". str_replace(' ', '', $contact->Value) ."\r\n";

    echo "\r\n";
}

结果如下:

Name: John d
Address: Lauvha 7, 87820
Value: 91000000

Name: Vis flere treff

您可以在此处查看示例:http://sandbox.onlinephpfunctions.com/code/ad6aa7fffaf42a32cb8f5568acb24c157c612467