简体   繁体   English

JSON和CURL编码

[英]JSON and CURL encoding

I have a curl page that output in json format but I can't get json reading from android correctly. 我有一个以json格式输出的curl页面,但是我无法正确地从android中读取json。 I want Json format with just an array only not object and array together. 我想要仅具有数组而不是对象和数组的Json格式。 Check the output at the bottom for example. 例如,检查底部的输出。 I want to write a php code that encode json that matches below. 我想写一个php代码,对下面匹配的json进行编码。 How would i do that? 我该怎么办? Thanks. 谢谢。

PHP FILE: PHP文件:

<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ .  '/vendor/mashape/unirest-php/src/Unirest.php';


//$response = Unirest\Request::get("",
//  array(
//    "host" => "",
//    "key" => "
//  )
//);
//
//
//echo stripslashes(json_encode($response));


$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "json",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_HTTPHEADER, array('Accept: application/json'),
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "host: ",
        "KEY: "
    ),
));


$response = curl_exec($curl);
$err = curl_error($curl);

$json = json_encode($response, JSON_UNESCAPED_SLASHES);


curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo stripslashes($response);
}

                        $fp = fopen('results.json', 'w');
                        fwrite($fp, stripslashes($response));
                        fclose($fp);

ORIGINAL OUTPUT FROM CURL: CURL的原始输出

{
    "quoteResponse": 
        {
        "result": [{
            "language": "en-US",
            "region": "US",
            "quoteType": "EQUITY",
            "quoteSourceName": "Nasdaq Real Time Price",
            "exchangeDataDelayedBy": 0,
            "regularMarketPrice": 30.69,
            "regularMarketTime": 1568404801,
            "regularMarketChange": 0.48000145,
            "regularMarketVolume": 45547913,
            "shortName": "Advanced Micro Devices, Inc.",
            "priceHint": 2,
            "postMarketChangePercent": 0.0325846,
            "postMarketTime": 1568419190,
            "postMarketPrice": 30.7,
            "postMarketChange": 0.0100002,
            "regularMarketChangePercent": 1.5888827,
            "regularMarketPreviousClose": 30.21,
            "fullExchangeName": "NasdaqGS",
            "longName": "Advanced Micro Devices, Inc.",
            "market": "us_market",
            "pageViews": {
                "shortTermTrend": "UP",
                "midTermTrend": "UP",
                "longTermTrend": "UP"
            },
            "marketState": "CLOSED",
            "exchange": "NMS",
            "sourceInterval": 15,
            "exchangeTimezoneName": "America/New_York",
            "exchangeTimezoneShortName": "EDT",
            "gmtOffSetMilliseconds": -14400000,
            "esgPopulated": false,
            "tradeable": true,
            "triggerable": true,
            "symbol": "AMD"
        }],
        "error": null
    }
}

THE OUTPUT I WANT IS THIS WITHOUT quoteResponse LIKE ABOVE OUTPUT. 我想要的输出没有quoteResponse输出。 CHECK BELOW : 检查以下内容:

{
    "result": [{
        "language": "en-US",
        "region": "US",
        "quoteType": "EQUITY",
        "quoteSourceName": "Nasdaq Real Time Price",
        "regularMarketPrice": 30.69,
        "regularMarketTime": 1568404801,
        "regularMarketChange": 0.48000145,
        "regularMarketVolume": 45547913,
        "sourceInterval": 15,
        "exchangeTimezoneName": "America/New_York",
        "exchangeTimezoneShortName": "EDT",
        "pageViews": {
            "shortTermTrend": "UP",
            "midTermTrend": "UP",
            "longTermTrend": "UP"
        },
        "gmtOffSetMilliseconds": -14400000,
        "market": "us_market",
        "postMarketChangePercent": 0.0325846,
        "postMarketTime": 1568419190,
        "postMarketPrice": 30.7,
        "postMarketChange": 0.0100002,
        "regularMarketChangePercent": 1.5888827,
        "priceHint": 2,
        "shortName": "Advanced Micro Devices, Inc.",
        "regularMarketPreviousClose": 30.21,
        "fullExchangeName": "NasdaqGS",
        "longName": "Advanced Micro Devices, Inc.",
        "exchange": "NMS",
        "exchangeDataDelayedBy": 0,
        "esgPopulated": false,
        "tradeable": true,
        "triggerable": true,
        "marketState": "CLOSED",
        "symbol": "AMD"
    }],
    "error": null
}

解码JSON,从中获取quoteResponse ,然后再次进行编码。

$json = json_encode(json_decode($json)->quoteResponse);

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

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