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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改