如何解析PHP中的json而不转换为数组?

时间:2017-04-28 09:21:06

标签: php arrays json

{
    "\u0000*\u0000collection_key": "rows",
    "\u0000*\u0000internal_gapi_mappings": [],
    "cacheHit": true,
    "\u0000*\u0000errorsType": "Google_Service_Bigquery_ErrorProto",
    "\u0000*\u0000errorsDataType": "array",
    "jobComplete": true,
    "\u0000*\u0000jobReferenceType": "Google_Service_Bigquery_JobReference",
    "\u0000*\u0000jobReferenceDataType": "",
    "kind": "bigquery#queryResponse",
    "pageToken": null,
    "\u0000*\u0000rowsType": "Google_Service_Bigquery_TableRow",
    "\u0000*\u0000rowsDataType": "array",
    "\u0000*\u0000schemaType": "Google_Service_Bigquery_TableSchema",
    "\u0000*\u0000schemaDataType": "",
    "totalBytesProcessed": "0",
    "totalRows": "1",
    "\u0000*\u0000modelData": {
        "schema": {
            "fields": [{
                "name": "total",
                "type": "INTEGER",
                "mode": "NULLABLE"
            }]
        },
        "jobReference": {
            "projectId": "qa-bigquery",
            "jobId": "job_y_qXq9mbzrnHzQZf_CUCr8itgmA"
        },
        "rows": [{
            "f": [{
                "v": "666750"
            }]
        }]
    },
    "\u0000*\u0000processed": []
}

我的脚本中有以上输出我需要遍历行和&得到价值{"v":"666750"}]?你能帮忙吗?

2 个答案:

答案 0 :(得分:1)

这不是特别优雅,但似乎确实有用。

首先,我从字符串中删除了所有\u0000*\u0000,然后只做了正常的json_decode()

$s = '{
    "\u0000*\u0000collection_key": "rows",
    "\u0000*\u0000internal_gapi_mappings": [],
    "cacheHit": true,
    "\u0000*\u0000errorsType": "Google_Service_Bigquery_ErrorProto",
    "\u0000*\u0000errorsDataType": "array",
    "jobComplete": true,
    "\u0000*\u0000jobReferenceType": "Google_Service_Bigquery_JobReference",
    "\u0000*\u0000jobReferenceDataType": "",
    "kind": "bigquery#queryResponse",
    "pageToken": null,
    "\u0000*\u0000rowsType": "Google_Service_Bigquery_TableRow",
    "\u0000*\u0000rowsDataType": "array",
    "\u0000*\u0000schemaType": "Google_Service_Bigquery_TableSchema",
    "\u0000*\u0000schemaDataType": "",
    "totalBytesProcessed": "0",
    "totalRows": "1",
    "\u0000*\u0000modelData": {
        "schema": {
            "fields": [{
                "name": "total",
                "type": "INTEGER",
                "mode": "NULLABLE"
            }]
        },
        "jobReference": {
            "projectId": "qa-bigquery",
            "jobId": "job_y_qXq9mbzrnHzQZf_CUCr8itgmA"
        },
        "rows": [{
            "f": [{
                "v": "666750"
            }]
        }]
    },
    "\u0000*\u0000processed": []
}';

$s1 = str_replace(array('\u0000*\u0000'), '', $s);
$j = json_decode($s1);
//echo json_last_error_msg();
//print_r($j);

// you now have a PHP object as defined by the original string

echo $j->modelData->rows[0]->f[0]->v;

RESULT

666750

答案 1 :(得分:0)

查看json_decode()函数。

<?php

$jsonString = '{"*collection_key":"rows","*internal_gapi_mappings":[],"cacheHit":true,"*errorsType":"Google_Service_Bigquery_ErrorProto","*errorsDataType":"array","jobComplete":true,"*jobReferenceType":"Google_Service_Bigquery_JobReference","*jobReferenceDataType":"","kind":"bigquery#queryResponse","pageToken":null,"*rowsType":"Google_Service_Bigquery_TableRow","*rowsDataType":"array","*schemaType":"Google_Service_Bigquery_TableSchema","*schemaDataType":"","totalBytesProcessed":"0","totalRows":"1","*modelData":{"schema":{"fields":[{"name":"total","type":"INTEGER","mode":"NULLABLE"}]},"jobReference":{"projectId":"qa-bigquery","jobId":"job_y_qXq9mbzrnHzQZf_CUCr8itgmA"},"rows":[{"f":[{"v":"666750"}]}]},"*processed":[]}';
$jsonObject = json_decode($jsonString);

var_dump($jsonObject->modelData->rows->f->v)