简体   繁体   English

Python 到 PHP | 字符串转多维 Json 对象转数组

[英]Python to PHP | String to Multidimensional Json Object To Array

I have a python script which returns an object as string.我有一个 python 脚本,它以字符串形式返回一个对象。 I call this python script with php and then print out the result with var_dump(json_decode($result)) and get this (this it the right object I want so my python code works properly I guess):我用 php 调用这个 python 脚本,然后用var_dump(json_decode($result))打印出结果并得到这个(这是我想要的正确对象,所以我猜我的python代码可以正常工作):

string(467) "{"userData": {"geburtsdatum": "PMS_2018-01-01", "anrede": "PMS_Herr", "ID": "1", "nachname": "PMS_Nachname1", "Test": {"tel": "PMS_Tel1", "postalOptIn": 0, "postal": "S3_Postal1", "email": "PMS_EMail1"}, "vorname": "PMS_Vorname1" }} " string(467) "{"userData": {"geburtsdatum": "PMS_2018-01-01", "anrede": "PMS_Herr", "ID": "1", "nachname": "PMS_Nachname1", "Test" : {"tel": "PMS_Tel1", "postalOptIn": 0, "postal": "S3_Postal1", "email": "PMS_EMail1"}, "vorname": "PMS_Vorname1" }} "

So as you can see its a string on php side.所以你可以在php端看到它的一个字符串。 But how can I convert it to an object now and the create a multidimensional array from it in php?但是我现在如何将它转换为一个对象并在 php 中从它创建一个多维数组?

If you need more information pls ask Ill add it.如果您需要更多信息,请询问我会添加它。

I tried:我试过:

json_decode($result, true);
json_decode($result);
$response = (array) $result;

all I get is an Array with 1 Index and the whole Object as Value in it.我得到的只是一个带有 1 个索引的数组,整个对象作为其中的值。

The Object gets generated like this on python side:对象在 python 端像这样生成:

for message in consumer:
        if message.key == str.encode(sys.argv[1]):
            returnValue = message.value #this here is an byte obj from external system

consumer.close()

print(returnValue.decode("latin-1"))

Edit 2 and Solution编辑 2 和解决方案

After long search I found out that the service I'm using (3d Party) returns the result from the python script with json_encode() .经过长时间的搜索,我发现我正在使用的服务(3d Party)使用json_encode()从 python 脚本返回结果。 I removed that and now this code works:我删除了它,现在此代码有效:

$array = json_decode($response, TRUE);

    return var_dump($array);

Since it is a string you can decode it like this:由于它是一个字符串,您可以像这样解码它:

$string =  '{"userData": {"geburtsdatum": "PMS_2018-01-01", "anrede": "PMS_Herr", "ID": "1", "nachname": "PMS_Nachname1", "Test": {"tel": "PMS_Tel1", "postalOptIn": 0, "postal": "S3_Postal1", "email": "PMS_EMail1"}, "vorname": "PMS_Vorname1" }}';

print_r(json_decode($string, true));

Which returns an array:它返回一个数组:

Array
(
    [userData] => Array
        (
            [geburtsdatum] => PMS_2018-01-01
            [anrede] => PMS_Herr
            [ID] => 1
            [nachname] => PMS_Nachname1
            [Test] => Array
                (
                    [tel] => PMS_Tel1
                    [postalOptIn] => 0
                    [postal] => S3_Postal1
                    [email] => PMS_EMail1
                )

            [vorname] => PMS_Vorname1
        )

)

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

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