简体   繁体   English

如何在php中将数组列表转换为JSON数组?

[英]How to convert a list of arrays to JSON array in php?

I am using fonoapi to display contents into my android app using volley. 我正在使用fonoapi使用齐射将内容显示到我的android应用中。 Volley requires data to be in JSON format while the php code returns data as an array(later modified by me but not getting proper JSON). Volley要求数据采用JSON格式,而php代码将数据作为数组返回(由我修改但未获得正确的JSON)。

Here is the php - 这是PHP-

<?php

    include_once("fonoapi-v1.php");

    $apiKey = "apiKey";
    $fonoapi = fonoApi::init($apiKey);

        try {

            $res = $fonoapi::getDevice("Samsung");

            foreach ($res as $mobile) {

                                $arr = [];
                                $arr['Device'] = $mobile->DeviceName;
                                $arr['Brand'] = $mobile->Brand;
                                $arr['Cpu'] = $mobile->cpu;
                                $arr['Status'] = $mobile->status;
                                $arr['Dimensions'] = $mobile->dimensions;
                                $arr['4g'] = $mobile->_4g_bands;
                        $json_arr = json_encode($arr);
                                echo $json_arr;

            }

        } catch (Exception $e) {
            echo "ERROR : " . $e->getMessage();
        }

?>

Here is the response i am getting from php - 这是我从php得到的响应-

{
    "Device": "Samsung SGH-250",
    "Brand": "Samsung",
    "Cpu": null,
    "Status": "Discontinued",
    "Dimensions": "124 x 48 x 25 mm (4.88 x 1.89 x 0.98 in)",
    "4g": null
} {
    "Device": "Samsung SGH-500",
    "Brand": "Samsung",
    "Cpu": null,
    "Status": "Discontinued",
    "Dimensions": "112 x 48 x 19 mm (4.41 x 1.89 x 0.75 in)",
    "4g": null
} {
    "Device": "Samsung SGH-600",
    "Brand": "Samsung",
    "Cpu": null,
    "Status": "Discontinued",
    "Dimensions": "108 x 44 x 21 mm (4.25 x 1.73 x 0.83 in)",
    "4g": null
}

There are more results, i am showing only 3. I want the proper json format required to be printed like shown below - 有更多结果,我只显示3。我希望正确的json格式需要打印,如下所示-

{
    domain : [
        {
            Device : "Samsung SGH-250",
            Brand : "Samsung",
            Cpu : null,
            Status : "Discontinued",
            Dimensions : "124 x 48 x 25 mm (4.88 x 1.89 x 0.98 in)",
            4g : null
        },
        {
            Device : "Samsung SGH-500",
            Brand : "Samsung",
            Cpu : null,
            Status : "Discontinued",
            Dimensions : "112 x 48 x 19 mm (4.41 x 1.89 x 0.75 in)",
            4g : null
        },
        {
            Device : "Samsung SGH-600",
            Brand : "Samsung",
            Cpu : null,
            Status : "Discontinued",
            Dimensions : "108 x 44 x 21 mm (4.25 x 1.73 x 0.83 in)",
            4g : null
        }
    ]
}

Try to gather all data before using echo: 在使用echo之前,请尝试收集所有数据:

$response = array();

    try {

        $res = $fonoapi::getDevice("Samsung");

        foreach ($res as $mobile) {

                            $arr = [];
                            $arr['Device'] = $mobile->DeviceName;
                            $arr['Brand'] = $mobile->Brand;
                            $arr['Cpu'] = $mobile->cpu;
                            $arr['Status'] = $mobile->status;
                            $arr['Dimensions'] = $mobile->dimensions;
                            $arr['4g'] = $mobile->_4g_bands;

                    $response[] = $arr;

        }

    } catch (Exception $e) {
        echo "ERROR : " . $e->getMessage();
    }

    echo json_encode(array('domain'=>$response));

You're echoing out one Json converted array at a time. 您一次回显一个Json转换的数组。 What you need to do is make one larger array that contains all of the data and then convert that one larger array. 您需要做的是制作一个包含所有数据的较大数组,然后转换该较大数组。

<?php

include_once('fonoapi-v1.php');

$apiKey = 'apiKey';
$fonoapi = fonoApi::init($apiKey);

try {

    $res = $fonoapi::getDevice('Samsung');

    $phones = array();

    foreach($res as $mobile) {
        $phones[] = array(
            'Device' => $mobile->DeviceName,
            'Brand' => $mobile->Brand,
            'Cpu' => $mobile->cpu,
            'Status' => $mobile->status,
            'Dimensions' => $mobile->dimensions,
            '4g' => $mobile->_4g_bands
        );
    }

    $json_arr = json_encode($phones);
    echo $json_arr;

} catch (Exception $e) {
    echo 'ERROR : ', $e->getMessage();
}

To achieve the example reference you provided you can do something like this (gist of the code you need to change): 为了获得您提供的示例参考,您可以执行以下操作(需要更改的代码要点):

$response = [];
$response['domain'] = [];

foreach ($res as $mobile)
{
    $arr = [];
    $arr['Device'] = $mobile->DeviceName;
    $arr['Brand'] = $mobile->Brand;
    $arr['Cpu'] = $mobile->cpu;
    $arr['Status'] = $mobile->status;
    $arr['Dimensions'] = $mobile->dimensions;
    $arr['4g'] = $mobile->_4g_bands;

    $response['domain'][] = $arr;
}

echo json_encode($response);

try this 尝试这个

$domain = array();
foreach ($res as $mobile) {

                            $arr = [];
                            $arr['Device'] = $mobile->DeviceName;
                            $arr['Brand'] = $mobile->Brand;
                            $arr['Cpu'] = $mobile->cpu;
                            $arr['Status'] = $mobile->status;
                            $arr['Dimensions'] = $mobile->dimensions;
                            $arr['4g'] = $mobile->_4g_bands;
                    //$json_arr = json_encode($arr);
                            //echo $json_arr;
$domain[] = $arr;

        }
echo json_encode(array('domain' => $domain));

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

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