在php中JSON解码失败
我试图用php从android应用程序中解码json,但它返回空字符串 这是我的密码在php中JSON解码失败,php,android,json,Php,Android,Json,我试图用php从android应用程序中解码json,但它返回空字符串 这是我的密码 $json="{account0Name: 'shamonsha665@gmail.com',account0Type: 'com.google',account1Name: 'mail.root5s@gmail.com',account1Type: 'com.google',account2Name: 'shamonwaste@gmail.com',account2Type: 'com.google',acc
$json="{account0Name: 'shamonsha665@gmail.com',account0Type: 'com.google',account1Name: 'mail.root5s@gmail.com',account1Type: 'com.google',account2Name: 'shamonwaste@gmail.com',account2Type: 'com.google',account3Name: '911.gksfapp@gmail.com',account3Type: 'com.google',account4Name: 'developer.root5@gmail.com',account4Type: 'com.google',account5Name: 'personal.root5@gmail.com',account5Type: 'com.google',account6Name: 'WhatsApp',account6Type: 'com.whatsapp',account7Name: 'shamonsha665@gmail.com',account7Type: 'com.linkedin.android',account8Name: '7736527089',account8Type: 'com.facebook.auth.login',deviceID: '911380450169267',phoneNo: '779808980',netCountry: 'in',netName: 'XXX',simNo: '8991197239007526600',simCountry: 'iX',simName: '!XX'}";
$owners=json_decode($json,true);
var_dump($owners);
var\u dump()
返回空值
这是我的json源代码
var deviceInfo = cordova.require("cordova/plugin/DeviceInformation");
deviceInfo.get(function(result) {
//get the plugin result
owner=JSON.stringify(result);
$.ajax({
type: 'POST',
url: 'http://ww.xxx.com/app/testjson.php',
data:{'owner':owner},
success: function(msg) {
alert("from server"+msg);
},
error:function(err){
alert("error"+JSON.stringify(err));
}
});
},, function() {
console.log("error");
});
你的Json字符串是错误的。你能像下面那样更改你的Json字符串吗 请尝试以下代码:-
<?php
$json='{"account0Name": "shamonsha665@gmail.com","account0Type": "com.google","account1Name": "mail.root5s@gmail.com",.........}';
$obj = json_decode($json, true);
print_r($obj);
?>
我认为这对您的需求很有用。@p>@its Root是因为您正在解码一个json字符串,而您一开始没有对其进行编码,所以它什么也不解码。试试这个:
`$json="{account0Name: 'shamonsha665@gmail.com',account0Type: 'com.google',account1Name: 'mail.root5s@gmail.com',account1Type: 'com.google',account2Name: 'shamonwaste@gmail.com',account2Type: 'com.google',account3Name: '911.gksfapp@gmail.com',account3Type: 'com.google',account4Name: 'developer.root5@gmail.com',account4Type: 'com.google',account5Name: 'personal.root5@gmail.com',account5Type: 'com.google',account6Name: 'WhatsApp',account6Type: 'com.whatsapp',account7Name: 'shamonsha665@gmail.com',account7Type: 'com.linkedin.android',account8Name: '7736527089',account8Type: 'com.facebook.auth.login',deviceID: '911380450169267',phoneNo: '779808980',netCountry: 'in',netName: 'XXX',simNo: '8991197239007526600',simCountry: 'iX',simName: '!XX'}";
$json = json_encode($json);
var_dump(json_decode($json));`
这完全有效。
字符串(738)”{account0Name:'shamonsha665@gmail.com,account0Type:'com.google',account1Name:'mail。root5s@gmail.com,account1Type:'com.google',account2Name:'shamonwaste@gmail.com,account2Type:'com.google',account3Name:'911。gksfapp@gmail.com,account3Type:'com.google',account4Name:'developer。root5@gmail.com,account4Type:'com.google',account5Name:'per索纳尔。root5@gmail.com,account5Type:'com.google',account6Name:'WhatsApp',account6Type:'com.WhatsApp',account7Name:'shamonsha665@gmail.com,account7Type:'com.linkedin.android',account8Name:'7736527089',account8Type:'com.facebook.auth.login',deviceID:'911380450169267',phoneNo:'779808980',netCountry:'in',netName:'XXX',simNo:'8991197239007526600',simCountry:'iX',simName:'!XX'}“
您的JSON无效
应该是这样的:
{"account0Name": "shamonsha665@gmail.com",...
等等。也许这有帮助
<?php
$json="{account0Name: 'shamonsha665@gmail.com',account0Type: 'com.google',account1Name: 'mail.root5s@gmail.com',account1Type: 'com.google',account2Name: 'shamonwaste@gmail.com',account2Type: 'com.google',account3Name: '911.gksfapp@gmail.com',account3Type: 'com.google',account4Name: 'developer.root5@gmail.com',account4Type: 'com.google',account5Name: 'personal.root5@gmail.com',account5Type: 'com.google',account6Name: 'WhatsApp',account6Type: 'com.whatsapp',account7Name: 'shamonsha665@gmail.com',account7Type: 'com.linkedin.android',account8Name: '7736527089',account8Type: 'com.facebook.auth.login',deviceID: '911380450169267',phoneNo: '779808980',netCountry: 'in',netName: 'XXX',simNo: '8991197239007526600',simCountry: 'iX',simName: '!XX'}";
$json = str_replace(array("'", ': "', ',', '{'), array('"', '": "', ',"', '{"'), $json);
$owners = json_decode($json);
print_r($owners);
?>
使用echo json_encode($json)它返回相同的字符串而不是数组对不起,我不明白你想在php中正确打印json吗?这是无效的
json
,这就是原因。检查它将告诉你如何以及为什么你的json
无效。字符串必须在双引号中。单引号无效。另外,使用json\u last\u error\u msg()
找出上一个错误是什么。如果您还有其他问题,请阅读PHP。解释您所做的。是的,这是有效的。您可以演示如何从该数组中获取每个元素吗?使用这个print_r(json_decode(stripslashes($json));。我使用explode来获取每个元素。就像这个伙伴$ser=json_decode($json);$j=explode(',',$ser);?>
输出类似于::数组(25){[0]=>string(39)“{account0Name:”shamonsha665@gmail.com“[1]=>string(26)”account0Type:'com.google'”[2]=>string(37)”account1Name:'mail。root5s@gmail.com“[3]=>string(26)“account1Type:'com.google”“[4]=>string(37)”account2Name:'shamonwaste@gmail.com'字符串(15)“simName:'!XX'}”}
由于注释框的长度,我删除了一些注释。您可以直接访问对象,或者通过先转换为数组来使用数组。请检查更新答案。
print_r($owners);
echo $owners->account0Name; // access object directly
$ownersArray = get_object_vars($owners); // convert to array
print_r($ownersArray);
echo $ownersArray['account0Name']; // and access by index