简体   繁体   English

如何从 json 内容中提取键和值?

[英]how to extract key and value from json content?

what I have tried is this.我试过的是这个。

public function toArrayss(){

        $json = '{"notificationType":"Bounce","bounce":{"feedbackId":"010201762c4054a2-424cab55-f6b7-420c-8f51-f1f13cb8b702-000000","bounceType":"Transient","bounceSubType":"General","bouncedRecipients":[{"emailAddress":"chintu@mailinatorc.com","action":"failed","status":"4.4.7","diagnosticCode":"smtp; 554 4.4.7 Message expired: unable to deliver in 840 minutes.<421 4.4.0 Unable to lookup DNS for mailinatorc.com>"}],"timestamp":"2020-12-04T05:36:22.000Z","reportingMTA":"dsn; a4-1.smtp-out.eu-west-1.amazonses.com"},"mail":{"timestamp":"2020-12-03T13:58:22.222Z","source":"ooionline@opsimplify.com","sourceArn":"arn:aws:ses:eu-west-1:128134649499:identity/opsimplify.com","sourceIp":"3.248.44.136","sendingAccountId":"128134649499","messageId":"0102017628e5910e-f8d2fba5-c25f-4109-8571-246ca3c983b2-000000","destination":["chintu@mailinatorc.com"],"headersTruncated":false,"headers":[{"name":"Received","value":"from sharabh.ooionline.com (ec2-3-248-44-136.eu-west-1.compute.amazonaws.com [3.248.44.136]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-d-8DMC1AGE7) id zcEZl2C63kMsDHYhlI2W for chintu@mailinatorc.com; Thu, 03 Dec 2020 13:58:22 +0000 (UTC)"},{"name":"Date","value":"Thu, 3 Dec 2020 14:58:22 +0100"},{"name":"To","value":"chintu@mailinatorc.com"},{"name":"From","value":"OPSimplify <ooionline@opsimplify.com>"},{"name":"Reply-To","value":"Email Notification <noreply@opsimplify.com>"},{"name":"Subject","value":"Votre accès à la plateforme collaborative SHARABH"},{"name":"Message-ID","value":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>"},{"name":"X-Mailer","value":"PHPMailer 6.0.7 (https://github.com/PHPMailer/PHPMailer)"},{"name":"MIME-Version","value":"1.0"},{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"commonHeaders":{"from":["OPSimplify <ooionline@opsimplify.com>"],"replyTo":["Email Notification <noreply@opsimplify.com>"],"date":"Thu, 3 Dec 2020 14:58:22 +0100","to":["chintu@mailinatorc.com"],"messageId":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>","subject":"Votre accès à la plateforme collaborative SHARABH"}}}
';
        $objs = json_decode($json,true);

        $key = ['notificationType','bounceType','bounceSubType','timestamp'];
        foreach ($objs as $obj => $values )  {
            // echo $obj . ":". $values;
            dd($objs);
            // echo $values ;

        }

Can anyone help me in extracting keys and values like notificationType,bounceType,timestamp etc;谁能帮我提取诸如notificationType、bounceType、timestamp等键和值; It would be great help for me if anyone help me achieving this.如果有人帮助我实现这一目标,那对我来说将是很大的帮助。 Thank you.谢谢你。

json_decode decode a string converting it in php data structures: array or object. json_decode 解码一个字符串,将其转换为 php 数据结构:数组或 object。 Check docs for json_decode .检查json_decode的文档。

You get object if you pass the second parameter as false:如果将第二个参数作为 false 传递,则会得到 object:

<?php

$decoded = json_decode(..., false);

echo $decoded->notificationType;

And get associative array if pass true as the second parameter如果将 true 作为第二个参数传递,则获取关联数组

<?php

$decoded = json_decode(..., true);

echo $decoded['notificationType'];

Your problem is that you are handling the returned array as a collection of notifications.您的问题是您将返回的数组作为通知集合处理。 Note that $objs only contains a single notification object.请注意,$objs 仅包含单个通知 object。

<?php
function toArrayss() {
    
    $json = '{"notificationType":"Bounce","bounce":{"feedbackId":"010201762c4054a2-424cab55-f6b7-420c-8f51-f1f13cb8b702-000000","bounceType":"Transient","bounceSubType":"General","bouncedRecipients":[{"emailAddress":"chintu@mailinatorc.com","action":"failed","status":"4.4.7","diagnosticCode":"smtp; 554 4.4.7 Message expired: unable to deliver in 840 minutes.<421 4.4.0 Unable to lookup DNS for mailinatorc.com>"}],"timestamp":"2020-12-04T05:36:22.000Z","reportingMTA":"dsn; a4-1.smtp-out.eu-west-1.amazonses.com"},"mail":{"timestamp":"2020-12-03T13:58:22.222Z","source":"ooionline@opsimplify.com","sourceArn":"arn:aws:ses:eu-west-1:128134649499:identity/opsimplify.com","sourceIp":"3.248.44.136","sendingAccountId":"128134649499","messageId":"0102017628e5910e-f8d2fba5-c25f-4109-8571-246ca3c983b2-000000","destination":["chintu@mailinatorc.com"],"headersTruncated":false,"headers":[{"name":"Received","value":"from sharabh.ooionline.com (ec2-3-248-44-136.eu-west-1.compute.amazonaws.com [3.248.44.136]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-d-8DMC1AGE7) id zcEZl2C63kMsDHYhlI2W for chintu@mailinatorc.com; Thu, 03 Dec 2020 13:58:22 +0000 (UTC)"},{"name":"Date","value":"Thu, 3 Dec 2020 14:58:22 +0100"},{"name":"To","value":"chintu@mailinatorc.com"},{"name":"From","value":"OPSimplify <ooionline@opsimplify.com>"},{"name":"Reply-To","value":"Email Notification <noreply@opsimplify.com>"},{"name":"Subject","value":"Votre accès à la plateforme collaborative SHARABH"},{"name":"Message-ID","value":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>"},{"name":"X-Mailer","value":"PHPMailer 6.0.7 (https://github.com/PHPMailer/PHPMailer)"},{"name":"MIME-Version","value":"1.0"},{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"commonHeaders":{"from":["OPSimplify <ooionline@opsimplify.com>"],"replyTo":["Email Notification <noreply@opsimplify.com>"],"date":"Thu, 3 Dec 2020 14:58:22 +0100","to":["chintu@mailinatorc.com"],"messageId":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>","subject":"Votre accès à la plateforme collaborative SHARABH"}}}
';
    $objs = json_decode($json, true);
    
    $key = ['notificationType', 'bounceType', 'bounceSubType', 'timestamp'];
    foreach ($objs as $obj => $values) {
        // echo $obj . ":". $values;
        dd($objs);
        // echo $values ;
        
    }
}

Modify your code like this像这样修改你的代码

<?php

public function toArrays($json) {
    
    // if the json string contains a collection of notifications
    // this line returns an array that contains multiple notifications
    // in the other hand, if the json string contains a single notification object
    // this line returns a single notification as array
    return json_decode($json, true);
}

In this way you receive the json content as parameter and toArrays method only decode it and returns as array.通过这种方式,您将收到 json 内容作为参数,而 toArrays 方法仅对其进行解码并作为数组返回。 You can iterate it with foreach, and can access to the indexes in the traditional way.您可以使用 foreach 对其进行迭代,并且可以以传统方式访问索引。

<?php

$asArray = $someObject->asArrays($someJsonString);

echo $asArray['some_property'];

foreach ($asArray as $key => $value) {
    echo ">" . $key;

    if (is_array($value)) {
        foreach ($value as $subKey => $subValue) {
            echo ">>>" . $subKey . "\n";
            echo ">>>>" . $subValue . "\n";
        }
    } else {
        echo ">>" . $value . "\n";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM