简体   繁体   English

如何从文件中的一行获取键值对

[英]how to get the key value pair from a line in a file

Hello.你好。 I have this line extracted from a file.我从文件中提取了这一行。

2021-03-08 13:44:11,241  INFO [192.168.34.10] - response data ==> {
  "result": {
    "vResultCode": "N001000001100000",
    "orderInfos": {
      "orderInfo": [
        {
          "index": 0,
          "lastSuccessTxnType": "Authorize",
          "orderId": "9040000123",
          "orderStatus": "end",
          "properOrderInfo": {
            "cardExpire": "000",
            "startTxn": "60979460"
          },
          "serviceTypeCd": "card",
          "successDetailTxnType": "a",
          "transactionInfos": {
            "transactionInfo": [
              {
                "amount": "560",
                "command": "Authorize",
                "mstatus": "success",
                "properTransactionInfo": {
                  "cardTransactionType": "a",
                  "centerRequestDate": "20210308134240",
                  "centerResponseDate": "20210308134240",
                  "connectedCenterId": "jcn",
                  "gatewayRequestDate": "20210308134240",
                  "gatewayResponseDate": "20210308134240",
                  "loopback": "0",
                  "pending": "0",
                  "reqAcquirerCode": "05",
                  "reqAmount": "560",
                  "reqCardExpire": "*****",
                  "reqCardNumber": "411111*11",
                  "reqItemCode": "0990",
                  "reqJpoInformation": "10",
                  "reqSecurityCode": "000",
                  "reqWithCapture": "false",
                  "resActionCode": "000",
                  "resAuthCode": "000000",
                  "resCenterErrorCode": "   ",
                  "resReturnReferenceNumber": "012345678901",
                  "txnKind": "card"
                },
                "txnDatetime": "2021-03-08 13:42:40.285",
                "txnId": "60979460",
                "vResultCode": "A001000000000000"
              }
            ]
          }
        }
      ]
    },
    "overMaxCountFlag": false,
    "searchCount": 1,
    "mstatus": "success",
    "serviceType": "search"
  }
}

How do i get the key value pairs from here?我如何从这里获取键值对? like i want to get the reqAmount which is '560' or the mstatus that is 'success' I'm using php laravel btw**就像我想获得“560”的 reqAmount 或“成功”的 mstatus 我正在使用php laravel btw**

Extract data from your response start with curly braces and do it like below.从您的响应中提取数据以花括号开头,然后按照下面的方式进行操作。 your response contains json part and some info regarding IP and date.您的回复包含 json 部分以及有关 IP 和日期的一些信息。 You can try it like this:你可以这样尝试:

$responseData = '{
    "result": {
        "vResultCode": "N001000001100000",
        "orderInfos": {
            "orderInfo": [
                {
                    "index": 0,
                    "lastSuccessTxnType": "Authorize",
                    "orderId": "9040000123",
                    "orderStatus": "end",
                    "properOrderInfo": {
                        "cardExpire": "000",
                        "startTxn": "60979460"
                    },
                    "serviceTypeCd": "card",
                    "successDetailTxnType": "a",
                    "transactionInfos": {
                        "transactionInfo": [
                            {
                                "amount": "560",
                                "command": "Authorize",
                                "mstatus": "success",
                                "properTransactionInfo": {
                                    "cardTransactionType": "a",
                                    "centerRequestDate": "20210308134240",
                                    "centerResponseDate": "20210308134240",
                                    "connectedCenterId": "jcn",
                                    "gatewayRequestDate": "20210308134240",
                                    "gatewayResponseDate": "20210308134240",
                                    "loopback": "0",
                                    "pending": "0",
                                    "reqAcquirerCode": "05",
                                    "reqAmount": "560",
                                    "reqCardExpire": "*****",
                                    "reqCardNumber": "411111*11",
                                    "reqItemCode": "0990",
                                    "reqJpoInformation": "10",
                                    "reqSecurityCode": "000",
                                    "reqWithCapture": "false",
                                    "resActionCode": "000",
                                    "resAuthCode": "000000",
                                    "resCenterErrorCode": " ",
                                    "resReturnReferenceNumber": "012345678901",
                                    "txnKind": "card"
                                },
                                "txnDatetime": "2021-03-08 13:42:40.285",
                                "txnId": "60979460",
                                "vResultCode": "A001000000000000"
                            }
                        ]
                    }
                }
            ]
        },
        "overMaxCountFlag": false,
        "searchCount": 1,
        "mstatus": "success",
        "serviceType": "search"
    }
}';
$jsonRes = json_decode($responseData, true); //json string to array
$newRes = $jsonRes['result']['orderInfos']['orderInfo']; 
$count = count($newRes);
$j=0;
for ($i = 0; $i < $count; $i++) {
    echo $newRes[$i]['transactionInfos']['transactionInfo'][$j]['amount'];
    $j++;
}

Output Output

560

We have extract your data and convert into array through json_decode function and get the amount through loop.我们已提取您的数据并通过json_decode function 转换为数组,并通过循环获取数量。 You can also get your output without loop, but there can be scenario where you will get multiple array objects.您也可以在没有循环的情况下获得 output,但在某些情况下您将获得多个数组对象。

Please replace $i and $j with 0 .请将$i$j替换为0

echo $jsonRes['result']['orderInfos']['orderInfo'][0]['transactionInfos']['transactionInfo'][0]['amount'];

Your extracted string basically contains some meta data (date, IP, etc.) followed by some JSON formatted data.您提取的字符串基本上包含一些元数据(日期、IP 等),然后是一些 JSON 格式的数据。

PHP has a built in function ( json_decode ) to handle converting JSON strings into PHP objects/arrays. PHP 有一个内置的 function ( json_decode ) 来处理将 JSON 字符串转换为 Z2FEC1392304A5C23 对象/数组2A7F8Z 字符串。 So we just need to strip out the the excess data and then use the function on the remaining string:所以我们只需要去掉多余的数据,然后在剩余的字符串上使用 function :

$string = <<<EOT
2021-03-08 13:44:11,241  INFO [192.168.34.10] - response data ==> {"result":{"vResultCode":"N001000001100000","orderInfos":{"orderInfo":[{"index":0,"lastSuccessTxnType":"Authorize","orderId":"9040000123","orderStatus":"end","properOrderInfo":{"cardExpire":"000","startTxn":"60979460"},"serviceTypeCd":"card","successDetailTxnType":"a","transactionInfos":{"transactionInfo":[{"amount":"560","command":"Authorize","mstatus":"success","properTransactionInfo":{"cardTransactionType":"a","centerRequestDate":"20210308134240","centerResponseDate":"20210308134240","connectedCenterId":"jcn","gatewayRequestDate":"20210308134240","gatewayResponseDate":"20210308134240","loopback":"0","pending":"0","reqAcquirerCode":"05","reqAmount":"560","reqCardExpire":"*****","reqCardNumber":"411111*11","reqItemCode":"0990","reqJpoInformation":"10","reqSecurityCode":"000","reqWithCapture":"false","resActionCode":"000","resAuthCode":"000000","resCenterErrorCode":" ","resReturnReferenceNumber":"012345678901","txnKind":"card"},"txnDatetime":"2021-03-08 13:42:40.285","txnId":"60979460","vResultCode":"A001000000000000"}]}}]},"overMaxCountFlag":false,"searchCount":1,"mstatus":"success","serviceType":"search"}}
EOT;

echo(
    json_decode(preg_replace("/^.*==> ?/", "", $string))
        ->result
        ->orderInfos
        ->orderInfo[0]
        ->transactionInfos
        ->transactionInfo[0]
        ->properTransactionInfo
        ->reqAmount
    );

Output Output

560

preg_replace

Uses a regular expression (first parameter) to match against the string (third parameter) and replaces the matched data with the replacement value (second parameter).使用正则表达式(第一个参数)匹配字符串(第三个参数),并用替换值(第二个参数)替换匹配的数据。

/^.*==> ?/
/           : Pattern delimiter
 ^          : Matches the start of the string
  .*        : Matches any character 0 or more times
    ==>     : Matches literally
        ?   : Matches an optional space
         /  : Pattern delimiter

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

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