【发布时间】:2019-04-04 15:05:10
【问题描述】:
我正在使用 angular 和 laravel,我的请求中有这个 json,但由于某种原因,我无法访问名为“llave_criptografica”的键中的数据,这是完整的 json
{username: "eeded", contrasena: null, llave_criptografica: {…}, clave_criptografica: null}
“llave_criptografica”里面的数据是这样的
{filename: "011503046233.p12", filetype: "application/x-pkcs12", value: "MIIaPAIBAzCCGfYG-base-64-encode"}
如您所见,我刚刚使用 base64 编码并发送了一个密钥,但是当我尝试访问它时,我收到了这个错误
return $request->llave_criptografica->filename;
试图获取非对象的属性
如果我尝试作为数组访问,这是错误
return $request->llave_criptografica["filename"];
JSON.parse 中位置 1 处的 JSON 中的意外数字
这是我存储文件数据的函数
onFileChange(event) {
let reader = new FileReader();
if(event.target.files && event.target.files.length > 0) {
let file = event.target.files[0];
reader.readAsDataURL(file);
reader.onload = () => {
this.formCertificado.get('llave_criptografica').setValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1]
})
}
}
}
我做日志的时候,这是key的内容
array ( 'username' => 'eded', 'contrasena' => NULL, 'llave_criptografica' => array ( 'filename' => '011503046233.p12', 'filetype' => 'application/x-pkcs12', 'value' => 'MIIaP-base-64', ), 'clave_criptografica' => NULL, )
【问题讨论】:
-
var_dump($request-> llave_criptografica);能得到什么?如果是另一个 json,则需要对其进行解码。 -
您在此处复制的示例不是有效的 JSON,例如键不在引号内。如果您可以发布确切的请求正文会很有帮助。
-
嗨,当我尝试解码时出现此错误,json_decode() 期望参数 1 为字符串,给定数组
-
请发送
dd($request->all())并编辑问题以包含该转储数据。 -
所以
llave_criptografica是一个数组,所以你需要用PHP中的数组方法来访问它,而不是对象。请注意,您的错误消息是两个不同的错误:Trying to get property of non-object来自 PHP,并通过正确访问它来修复。Unexpected number in JSON at position 1 at JSON.parse是一个 javascript 错误,可能是因为您返回的是字符串,而不是 json。