我正在尝试使用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',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_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");
});
答案 0 :(得分:1)
也许这有帮助。
<?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);
?>
注意:对于另一个json,str_replace可能无法正常工作。
更新:获取价值
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
答案 1 :(得分:0)
@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));`
这完全有效。
string(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: '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'}"
答案 2 :(得分:0)
您的JSON无效。
应该是这样的:
{"account0Name": "shamonsha665@gmail.com",...
等等。
答案 3 :(得分:-1)
您的Json字符串错误。您可以更改您的json字符串,如下所示。
试试这段代码: -
<?php
$json='{"account0Name": "shamonsha665@gmail.com","account0Type": "com.google","account1Name": "mail.root5s@gmail.com",.........}';
$obj = json_decode($json, true);
print_r($obj);
?>
我认为这对您的要求很有用。