I want to send whole json array on a link so i have to send whole json array on right move ? because in it's format they give us a json_array format.
Below is the json array of rightmove,
Json Array ,
{
"network":{
"network_id": 5
},
"branch":{
"branch_id": 1566,
"channel": 1,
"overseas": false
},
"property":{
"agent_ref": "02072013_0406",
"published": true,
"property_type": 2,
"status": 1,
"new_home": false,
"student_property": false,
"create_date": "02-07-2013 00:00:00",
"update_date": "02-07-2013 00:00:00",
"date_available": "02-07-2013 00:00:00",
"contract_months": 12,
"minimum_term": 12,
"let_type": 1,
"address":{
"house_name_number": "33",
"address_2": "Rightmove",
"address_3": "4th Floor",
"address_4": "Soho Square",
"town": "London",
"postcode_1": "W1D",
"postcode_2": "3QU",
"display_address": "Soho Square",
"latitude": 51.514899,
"longitude": -0.132587,
"pov_latitude": 51.51482,
"pov_longitude": -0.13249,
"pov_pitch": -16.78,
"pov_heading": 235.75,
"pov_zoom": 0
},
"price_information":{
"price": 1500,
"price_qualifier": 0,
"deposit": 1000,
"administration_fee": "100",
"rent_frequency": 1,
"tenure_type": 1,
"auction": false,
"tenure_unexpired_years": 999,
"price_per_unit_area": 10
},
"details":{
"summary": "Rightmove Test Property",
"description": "Testing full property schema with all the fields in the call for JSON",
"features": [
"Has own Drive",
"Garage included",
"Double Glazed"
],
"bedrooms": 2,
"bathrooms": 2,
"reception_rooms": 1,
"parking": [13],
"outside_space": [29],
"year_built": 999,
"internal_area": 100,
"internal_area_unit": 1,
"land_area": 100,
"land_area_unit": 1,
"floors": 5,
"entrance_floor": 1,
"condition": 1,
"accessibility": [42],
"heating": [1],
"furnished_type": 0,
"pets_allowed": true,
"smokers_considered": true,
"housing_benefit_considered": true,
"sharers_considered": true,
"burglar_alarm": true,
"washing_machine": true,
"dishwasher": true,
"all_bills_inc": true,
"water_bill_inc": true,
"gas_bill_inc": true,
"electricity_bill_inc": true,
"tv_licence_inc": true,
"sat_cable_tv_bill_inc": true,
"internet_bill_inc": true,
"business_for_sale": true,
"comm_use_class":[1,4],
"rooms": [ {
"room_name": "room1",
"room_description": "room1" ,
"room_length": 10.10,
"room_width": 20.20,
"room_dimension_unit": 5,
"room_photo_urls": ["http://www.rightmove.com/image1.JPG"]
} ]
},
"media": [ {
"media_type":1,
"media_url":"www.rightmove.com/image1.JPG",
"caption":"This is an image",
"sort_order":1,
"media_update_date": "02-07-2013 12:12:12"
} ],
"principal": {
"principal_email_address": "principal#rightmove.co.uk",
"auto_email_when_live": true,
"auto_email_updates": true
}
}
}
I got every thing each and every parameter of it.
Now, rightmove api use post method and url is,
https://adfapi.rightmove.co.uk/v1/property/sendpropertydetails
So i'm trying to send json data to rightmove but it's showing me error. I have stored whole json Array in to $json_array.
jQuery.ajax({
url: 'https://adfapi.rightmove.co.uk/v1/property/sendpropertydetails/',
type : "POST",
crossDomain: true,
dataType: "jsonp",
data: {data : <?php echo $json_array; ?>},
success: function(data) {
alert(data);
}
});
});
I have also added in header but it shows me 403 error.
EDIT with error : it showing failure in response but in localhost i got response in local file so i think ajax call is working properly.
The problem is that the "print_r" funktion isnt creating a JSON-Objekt
As explained on php.net
<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y', 'z'));
print_r ($a);
?>
Would create
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
The solution is to use json_encode instead of print_r
so you only have to change
data : {data :<?php print_r($json_array); ?> }
to
data : {data :<?php json_encode($json_array); ?> },
I am assuming your javascript is generated each time in your PHP script as you dynamically build the webpage!
If your $json_array variable is already a JSON String, like the JOSNString you show above this is wrong
data : {data :<?php print_r($json_array); ?> },
and should be
data : {data :<?php echo $json_array; ?> },
If the data is held in a php array i.e. not already a JSONString then you should do
data : {data :<?php json_encode($json_array); ?> },
Related
I've got the following JSON array with PHP:
"data": [
{
"RecordID": 1,
"OrderID": "53150-422",
...
},
{
...
},
I do this with:
echo json_encode(array('data' => $users),JSON_PRETTY_PRINT);
Now I want to add metadata to the array like this:
{
"meta": {
"page": 1,
"pages": 1,
"perpage": -1,
"total": 350,
"sort": "asc",
"field": "RecordID"
},
"data": [
{
"RecordID": 1,
"OrderID": "53150-422",
...
},
How to to this? Thanks in advance!
write code this way
<?php
$meta=[ "page"=> 1,
"pages"=> 1,
"perpage"=> -1,
"total"=> 350,
"sort"=> "asc",
"field"=> "RecordID"];
$users= [
"RecordID"=> 1,
"OrderID"=> "53150-422"
];
echo json_encode(array('meta'=>$meta,'data' => $users),JSON_PRETTY_PRINT);
?>
My data table that is loading its body from another file with ajax is giving me the invalid JSON error, but when I check my developer tools under network responses my JSON is valid?
This is my PHP and SQL:
<?php
header('Content-Type: application/json');
$output = array('data' => array());
$query = "SELECT * FROM table";
$stmt = sqlsrv_query($sapconn2, $query);
$x = 1;
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
$output['data'][] = array(
'col_1' => $x,
'ID' => $row['ID'],
'QuoteID' => $row['QuoteID'],
'CardCode' => $row['CardCode'],
'SlpCode' => $row['SlpCode'],
'SlpName' => $row['SlpName'],
'BandA' => $row['BandA'],
'NewPrice' => $row['NewPrice']
);
$x ++;
}
echo json_encode($output);
?>
This is my JSON thats returned in the browser:
{
"data": [
[1, 138, 25, "000123", "222", "test data", 222, 222],
[2, 144, 25, "000123", "132", "test data", 465, 789],
[3, 160, 25, "000123", "456132", "test data", 5599, 5499],
[4, 171, 25, "000123", "789", "test data", 7897, 989],
[5, 172, 25, "000123", "11111", "test data", 1, 11],
[6, 182, 25, "000123", "132166", "test data", 1323, 133],
[7, 183, 25, "000123", "135456", "test data", 1332132, 13213],
[8, 184, 25, "000123", "1321", "test data", 5643214, 6513]
]
}
EDIT:
var testTable = $("#testTable").DataTable({
processing: false,
serverSide: true,
dataType : 'json',
ajax: "test.php",
columns: [
{ "data": "col_1" },
{ "data": "ID" },
{ "data": "QuoteID" },
{ "data": "CardCode" },
{ "data": "SlpCode" },
{ "data": "SlpName" },
{ "data": "BandA" },
{ "data": "NewPrice" }
]
});
This is what datatable waits for:
{
"data": [
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
...
]
}
The "data" element is an array of objects, instead you pass an array of array.
You need something like that:
{
"data": [
{ "id": 1, "second_field": 138, "third_field": 25, "fourth_field": "000123", ... },
{ "id": 2, "second_field": 138, "third_field": 25, "fourth_field": "000123", ... },
]
}
EDITED:
$output['data'][] = array(
'col_1' => $x,
'col_2' => $row['ID'],
'col_3' => $row['QuoteID'],
'col_4' => $row['CardCode'],
'col_5' => $row['SlpCode'],
'col_6' => $row['SlpName'],
'col_7' => $row['BandA'],
'col_8' => $row['NewPrice']
);
When you make a request to a server-side script from DataTables with processing set to true then it sends this data.
When it returns data DataTables expects the data to follow these conventions.
You can either take these into account with your server-side script (there's a good example here.) or choose a different method for adding your data. If you perhaps set processing to false you might find everything just works as you expect.
Hope that helps.
I am using the following code to display an external API in json format.
<?php
$result = file_get_contents('compress.zlib://https://URL', false, stream_context_create(array(
'http' => array(
'method' => 'GET'
)
))
);
echo($result);
?>
The result is like:
{
"product": [{
"product_name": "paper",
"product_no": "67",
"price": "7"
}, {
"product_name": "pencil",
"product_no": "69",
"price": "5"
}, etc]
}
However, if for example the value of the corresponding key "product_no" is empty, the key in that specific array is not visible anymore in json. I would like it to display "product_no":"", or something like that...
I need to query based on key's that all equal the same thing, let's say the following is an order, and that order contains three products. I need to query orders, that have products, that have a specific status all equal to true. Each product is stored, with a mongo id as its key, so I don't actually know it's key name, Example: (obviously I've shortened the keys)
{
"_id" : "foo",
"products": {
"123": {
"status": {
"a": false,
"b": true,
"c": true,
},
},
"213": {
"status": {
"a": true,
"b": true,
"c": true,
},
},
"321": {
"status": {
"a": false,
"b": false,
"c": true,
},
}
},
}
Here's what I've tried:
$this->database->$collection->find(
array('_id' => 'foo', 'products.$.status.c' => true)
);
I'd expect the above, to return the complete order in the example, as the status 'c' inside each product is true, if I were to perform the same query, but with 'a' or 'b' as the status query, it wouldn't return it.
I'm not really sure how to do this, obviously the above didn't work, so my question is, how can I match on multiple sub keys of an array of objects that I do not know the key name?
You want an array of documents, not an object with the id as its key. The $ positional operator doesn't work on objects.
{
"_id" : "foo",
"products": [
{
id: "123"
"status": {
"a": false,
"b": true,
"c": true,
},
},
{
id: "213"
"status": {
"a": true,
"b": true,
"c": true,
},
},
{
id: "321"
"status": {
"a": false,
"b": false,
"c": true,
},
}
]
}
Your query needs to look like this:
$this->database->$collection->find(
array('_id' => 'foo', 'products.status.c' => true)
);
This returns the cursor at the first 'product' that matches your query
and on udpate.
$this->database->$collection->find(
array('_id' => 'foo', 'products.status.c' => true),
array('products.$.hello' => 'world')
);
This updates only that sub document. The only time you should use objects is if you know what the keys will be. Like status, or address... but if key is a variable as well, it gets really tricky, really quickly.
I have the following array (exposed using var_dump).
array (size=3)
'auth' => string 'debug' (length=5)
'url' => string 'http://X.X.X.X/status.cgi?' (length=31)
'page' => string '{ "host": { "uptime": 1543, "time": "2011-07-26 12:07:40", "fwversion": "v1.1.1", "hostname": "ASDASDASD", "netrole": "DFDFDF" }, "lan": { "status": [{ "plugged": 1, "speed": 100, "duplex": 1 }], "hwaddr": "00:00:22:11:11:11", "ip": "", "rx": { "bytes": 5988, "packets": 83, "errors": 0 }, "tx": { "bytes": 9496, "packets": 120, "errors": 0 } }, "wan": { "status": [], "hwaddr": "00:00:00:00:00:00", "ip": "", "rx": { "bytes": 0, "packets": 0, "errors": 0 }, "tx": { "bytes": 0, "packets"'... (length=1779)
I need to extract info from WAN like ip or rx...
I have tried using $array['page']['wan']['rx'] but nothing!!!
Thanks.
The page value of your $array is not actually an array, but a string. Since it's a JSON string, you should use json_decode to decode the string into a meaningful format for PHP to handle:
$page = json_decode($array['page']);
$pageArray = json_decode($array['page']); // Returned as an array
And to get the RX for example:
var_dump($page->wan->rx); // Returned as an object
var_dump($pageArray['wan']['rx']); // Return as an array