duanhao7786 2019-02-01 22:32
浏览 123
已采纳

从JSON数组获取电子邮件

Have been trying to figure this out for a day now. I am trying to use fiken.no`s API to retrieve customernumbers. The API is HAL+JSON based and i want to access the data with PHP.

When creating a new account you receive no response, but only HTTP 201, so to get the generated customernumber i need to use the search endpoint.

That endpoint only returns all users like this:

{
"_links": {
    "self": {
        "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts"
    }
},
"_embedded": {
    "https://fiken.no/api/v1/rel/contacts": [
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/757941482"
                }
            },
            "name": "Ola Nordmann 2",
            "email": "mail@mail.com",
            "address": {
                "country": "Norge"
            },
            "customerNumber": 10003
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/757941171"
                }
            },
            "name": "Ola Nordmann 1",
            "email": "findthis@example.com",
            "address": {
                "country": "Norge"
            },
            "customerNumber": 10002
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/756867201"
                }
            },
            "name": "Demoleverandør",
            "address": {
                "address1": "Demoveien 44",
                "address2": "",
                "postalPlace": "Oslo",
                "postalCode": "0190",
                "country": "Norge"
            },
            "supplierNumber": 20001
        },
        {
            "_links": {
                "self": {
                    "href": "https://fiken.no/api/v1/companies/fiken-demo-personlig-gnist-enk/contacts/756867200"
                }
            },
            "name": "Demokunde",
            "address": {
                "address1": "Demoveien 22",
                "address2": "",
                "postalPlace": "Oslo",
                "postalCode": "0190",
                "country": "Norge"
            },
            "customerNumber": 10001
        }
    ]
}}

From this response i need to query for f.ex the email findthis@example.com and get the whole user object from that. That includes the address data and especially the customernumber. How would i go ahead to do that?

I have found this: Search a key in a Json (nested array) PHP that looks like my issues but here the key is constant in the json array. Here it is from 0 and out to infinity.

Is there any better way to process this using PHP than normal JSON practices? Thanks for any help!

  • 写回答

1条回答 默认 最新

  • dongmacuo1193 2019-02-01 23:06
    关注

    First thing would be to decode the JSON into an array:

    $resultArray = json_decode($yourJSONvariable, true);
    

    Next I would recommend isolating the array of contacts from the JSON array:

    $contacts = $resultArray["_embedded"]["https://fiken.no/api/v1/rel/contacts"];
    

    Now $contacts should be an array of all the contacts from the API call. You can loop through each one to find the record with the matching email like this:

    foreach ($contacts as $contact) {
        if ($contact["email"] == "findthis@example.com") {
            $mycontact = $contact;
            break;
        }
    }
    

    $mycontact will now contain an array of the contact with the matching email, and you can access its individual fields using the field names as the array index (e.g., $mycontact["name"], $mycontact["address"]["country"], $mycontact["_links"]["self"]["href"]). To see all data in the array do var_dump($contacts)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题