dongqia3502
dongqia3502
采纳率100%
2017-04-20 22:33 阅读 1.6k

如何在PHP中获取[object Object]的内容?

I我有一些数据,这些数据通过一些JSON发送并上传到数据库。 当我尝试将其写入数据库时,该字段为[object Object]。 JSON如下所示::

{
    "version":"1.0",
    "event":"video_recorded",
    "data":{
        "videoName":"vsrtc1492808825_223",
        "audioCodec":"NellyMoser ASAO",
        "videoCodec":"H.264",
        "type":"FLV",
        "orientation":"landscape",
        "id":"315414",
        "dateTime":"2017-04-21 14:07:12",
        "timeZone":"America/Los_Angeles",
        "payload":"{\\\"ip\\\":111.111.111.11, \\\"env\\\": test}",
        "httpReferer":"https://app.surveygizmo.com/projects/previewbottom?id=3489282&__sgtarget=3&sLanguage=English&__sg_build_test=1&__sg_collab_test=1&__sg_tester=SW5mb0BNb29uc0FuYWx5dGljcy5jb20%3D&__sg_tester_name=TW9vbnMgQW5hbHl0aWNz&__sg_tester_id=351560&link_id=0&__sg_skip_actions=1&__ignore_entry_logic=1&?preview_frame=true"
    }
}

在嵌入代码JavaScript中,我将此变量传递到flashvars中,以将其添加到json中:

var clientIP = '11.11.111.1111';
var payload_data = {"ip":clientIP, "env":"test"};
var flashvars = {qualityurl: "avq/300p.xml",accountHash:"randomstuff", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: payload_data};

我尝试在解码整个JSON之后选择正确的密钥:

$data = $_POST['payload'];
$retrieved_data = json_decode($data, true);
$ip_address = $retrieved_data['data']['payload'];

当我将$ ip_address写入数据库时,它返回:

[object Object]

如果我尝试在我的PHP中更深入一点:

$ip_address = $retrieved_data['data']['payload']['ip'];

它将四个条目添加到我的数据库中:

[
]
{ 
}

记录器的文档https://addpipe.com/docs#sending-custom-data-using-payload-variable说在单引号之间传递自定义JSON:

payload: '{"ip":clientIP, "env":"test"}'

如果我尝试将整个有效负载写入数据库中:

$ip_address = $retrieved_data['data']['payload'];

它将以下内容写入该字段,而不显示clientIP变量的内容:

{\"ip\":clientIP,\"env\":\"test\"}

如果我尝试用这个方法更深入一层:

$ip_address = $retrieved_data['data']['payload']['ip'];

我得到了相同的4项

[
]
{
}

所以我被卡住了,我不知道还能尝试什么。 我正在尝试获取此处的信息:

"payload":"{\\\"ip\\\":111.111.111.11, \\\"env\\\": test}"
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doumalu9257 doumalu9257 2017-04-21 21:44

    According to the documentation you've provided a link to:

    You have two formating options in order to send the JSON correctly:

    • enclose the JSON in quotes and write the JSON normally: payload:'{"a":"b"}'
    • enclose the JSON in double quotes and escape the double quotes from inside the JSON payload:"{\"a\":\"b\"}"

    This seems very clear:

    var clientIP = '11.11.111.1111';
    var payload_data = JSON.stringify({ip:clientIP, env:"test"});
    var flashvars = {qualityurl: "avq/300p.xml",accountHash:"randomstuff", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: payload_data};
    

    However this is apparently not working. Until the issue is resolved on the remote server, you can try sending a delimited string and then parse it on the PHP side:

    var clientIP = '11.11.111.1111';
    var payload_data = clientIP + '|test';
    var flashvars = {qualityurl: "avq/300p.xml",accountHash:"randomstuff", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: payload_data};
    

    Then in the PHP

    $data = $_POST['payload'];
    $retrieved_data = json_decode($data, true);
    list($ip_address, $env) = explode("|", $retrieved_data['data']['payload']);
    
    点赞 评论 复制链接分享
  • dongse5408 dongse5408 2017-04-20 22:46

    I've seen this sort of thing before, basically a JSON string stored as a string value for a key in a JSON object. PHP treats this nested JSON string as a string, and doesn't decode it on the first pass, so you have to do that separately. Try this:

    $data = $_POST['payload'];
    $retrieved_data = json_decode($data, true);
    $ip_address_json = $retrieved_data['data']['payload'];
    
    // Now decode the nested JSON string
    $ip_address = json_decode($ip_address_json, true);
    echo $ip_address['ip'];
    
    点赞 评论 复制链接分享

相关推荐