我有这样的网址:
http://localhost/datas.php?[{ id :27,latt:8.55699,ltd:76.882,tm:11:46:51}, {
id :97,latt:8.55699,ltd:76.882,tm:11:46:52}, { id
:31,latt:8.55699,ltd:76.882,tm:11:46:52}, { id
:96,latt:8.55703,ltd:76.8815,tm:11:53:22}]
我需要使用php
将所有这些值输入数据库我尝试了很多方法。但是没办法,它不起作用。
到目前为止,我做到了这一点。
$response = array();
$response["data"] = $array;
$json = json_encode($response);
var_dump($json);
但它显示数据为空。
请帮我解决这个问题
答案 0 :(得分:1)
永远不会通过URL传递json,您可以将其转换为字符串,然后按如下方式对其进行解码:
test.php的
private void addContact2() {
final String displayName = "XYZA";
final String mobileNumber = "666666";
final byte[] photoByteArray; // initalized elsewhere
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "")
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
//.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photoByteArray)
.build());
Uri newContactUri = null;
ContentProviderResult[] res = null;
try {
final ContentResolver contentResolver = getContentResolver();
res = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
if (res != null && res.length > 0 && res[0] != null) {
newContactUri = res[0].uri;
Log.d(CallActivity.class.getName(), "URI added contact:"+ newContactUri);
Toast.makeText(this, "Successfully added " + displayName, Toast.LENGTH_LONG).show();
}
else Log.e(CallActivity.class.getName(), "Contact not added.");
} catch (NullPointerException | RemoteException | OperationApplicationException e) {
Log.e(CallActivity.class.getName(), e.getMessage(), e);
}
}
test1.php
<?php
$json = '[{
"id": 27,
"latt": 8.55699,
"ltd": 76.882,
"tm": "11: 46: 51"
}, {
"id": 97,
"latt": 8.55699,
"ltd": 76.882,
"tm": "11: 46: 52"
}, {
"id": 31,
"latt": 8.55699,
"ltd": 76.882,
"tm": "11: 46: 52"
}, {
"id": 96,
"latt": 8.55703,
"ltd": 76.8815,
"tm": "11: 53: 22"
}]';
echo 'http://localhost/test.php?data='.base64_encode($json);
?>
你得到的结果将是:
<?php
$getdata = $_GET['data'];
$cjson = json_decode(base64_decode($getdata));
print_r($cjson);
?>
json的格式也不正确。请使用经过验证的json。
答案 1 :(得分:0)
尝试如下。
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->access_token;
答案 2 :(得分:0)
Atlast找到了解决方案。
$a = $_REQUEST['data'];
$bb = json_decode($a,true);
foreach ($bb as $res=>$value) {
$uid = $value["id"];
.
.
.
}