doubi2014 2014-08-06 14:26
浏览 59
已采纳

PHP脚本以错误的格式提取信息? 试着用JSON来获取它

I have a script in Python pulling information from one service and a script in PHP pulling from another. I'd like to parse and compare the data from both, so obviously I need the information to be in the same format.

The Python script returns a text file in JSON format. Here's a small snippet. Disregard the information inside. I just want to show the formatting.

{
    "category": 0,
    },
    "cluster_seed": "24",
    "clusters": {
        "clusters": [
            {
                "center": {
                    "000002": [
                        "massachusetts",
                        "new hampshire",
                        "new jersey"
                    ],
                    "000003": [
                        "Windows",
                        "OSX"
                    ]
                }

etc...

However, the PHP script returns information like this:

Array
(
    [0] => Array
        (
            [name] => John Smith
            [engagementScore] => 0
            [firstActivity] => 1386555168108
            [lastActivity] => 1386555168108
            [attributes] => Array
                (
                    [_deviceType] => Tablet
                    [_os] => Android
                    [_browser] => Chrome
                )

            [segmentMemberships] => Array
                (
                    [0] => Array
                        (
                            [segmentationName] => example
                            [segementationId] => example
                        )

                )

I'm not really sure what's going on here. I guess it's printing as a PHP object/array rather than a raw JSON file? I would like it to be formatted exactly as the first snippet is. This is the code in my PHP that pulls this information:

$response = curl_exec($session);
$info = curl_getinfo($session);

$body = substr($response, $info['header_size']);
$decoded_result = json_decode($body, true);

I thought that json_decode would print it in the same format but clearly that's not the case. I tried json_encode but that just prints an enormous block of completely illegible information.

Is there any way to print this information in the same format that the first script returns so I can compare the two? Thanks!

  • 写回答

2条回答 默认 最新

  • duangeli1334 2014-08-06 14:31
    关注

    This is the main purpose of json_decode: take a string in JSON format and convert it to either a PHP array or PHP StdClass object, so I'm not sure what your issue is exactly.

    From your question, I think what you're trying to do is (in PHP) fetch results from some service, then fetch results from your Python script and compare the two. You should json_decode both results and then you have two neat PHP arrays that are easily traverseable in order to perform your comparison rather than two strings that would be extremely difficult to compare as such.

    Think of json_decode as PHP's equivalent to Python's json.load

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗