As an API response to a payment gateway I receive an object, I would like it to become json so I used json_encode, but I only get the first key.
json_encode convert only the first key cause array is too large
json_encode convert array to json
json_decode convert json to array
API’s by default must give you a json and you must decode json data to array if you are going to use it on back-end.
Show some code, so we can help you
Related
Get all value from json using json_decode. Given name is Juliet"es this json. If using json_decode this array will be change null value. how do i get this json to array
$jsonobj = '{"Name":"Juliet"es","Maths":37,"English":43}';
if anyone having any idea please post answer.
Try $convertedJson = json_decode(addslashes($jsonobj));.
But has suggested by #Barmar and #CBroe , always generated JSON strings from array or object with json_encode().
I want to convert an array of objects to JSON encoding, but how it is possible to encode PHP array into JSON array.
if you want to convert php array into json
use:
json_encode($array);
json_encode
for decode
json_decode($array);
json_decode
I have an array with multiple items in it one of which is an already encoded json string. I'm wanting to json encode the whole array but in doing so it re-json_encodes the json and adds slashes to it. The only way I've found to fix this is to json_decode the value and then encode the whole array. I feel like this is a waste of resources though and I feel like there has to be a better way. Is doing it that way the best possible way?
Here's a sample of the array I'm trying to json_encode.
$arr = array();
$arr["var1"] = '{"test":"test"}';
$arr["var2"] = 'foo';
$arr["var3"] = 'bar';
If I don't decode the var1 first and I just encode the whole array I get a result like so
{"var1":"{\"test\":\"test\"}","var2":"foo","var3":"bar"}
Notice the slashes in the json object.
json_encode() returns a string containing the json representation of a value.
In the example, a php string is passed as one element of the array '{"test":"test"}', thus json_encode() is encoding it appropriately into json format, with escaped quotes "{\"test\":\"test\"}".
If decoding nested json is a very resource heavy task, a workaround is to use a placeholder instead of the value, {"var1":"PLACEHOLDER","var2":"foo","var3":"bar"}, and then using str_replace() to replace it.
However, simply decoding it as you described is probably a cleaner solution, if its not resource heavy.
I am making a call to an API which should return a JSON array. I receive this:
Array
(
[0] => {"msg_id":"0t5OxT1ZP9VcF45L2","_text":"I want to reserve","entities":{"intent":[{"confidence":1,"value":"make_reservation","type":"value"}]}}
)
I know it's JSON because a) the docs say that's what I should be getting and b) I ran isJson($response) and got true.
I have tried to use json_decodebut the code just dies when I do (it errs saying it's expecting a string and got an array which makes sense but if I do json_encode that would just further encode the json from what I can understand).
As I understand it, I just need a way to traverse this array and get the "value:" key inside entities: intent:. However I can't figure out how to get it or where I'm wrong.
I have tried doing:
$val = $jsonArray[0]['entitites']['intent'][0]['value'] but nothing comes out.
You are trying to decode a PHP array that has encoded values.
You should try json_decode($jsonArray[0]) instead, so that you decode the value of the first array key, as that is the actual json string.
The data you posted is a php array where the value of the first element of the array is a json string.
json_decode($response[0]);
I currently have json using json_encode from a mysql query which looks like this:
{"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}, {"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}
How can I have the json being an array of posts ('post_2', 'post_1') rather than it being a dictionary? The JSON will be decoded on an iPhone using SBJSON and the JSON will have to be made into an array in the backend.
Thanks in advanced.
Provide a non-associative array to json_encode(). The easiest way is usually to simply call array_values() on the (associative) array, and encode the result.
Take a look at PHP's json_decode function, specifically the 2nd parameter if you want an array.