Why json_decode() return empty? - php

I have this JSON string:
$json = '
{"id1":"x",
"N1":"x",
"T1":"x",
"E1":"x",
"E11":"x",
"O1":"x",
"C1":"x",
"C11":"x",
"F1":"x"},
{"id2":"x",
"N2":"x",
"T2":"x",
"E2":"x",
"E22":"x",
"O2":"x",
"C2":"x",
"C22":"x",
"F2":"x"}
';
and after I parse with PHP:
print_r(json_decode($json));
and you can see PHP return empty.
Any help, please...
Thanks

That's not really valid JSON syntax, if you intend that to be two objects, you need to put them in an array.
Like [
{your first object here},
{your next object}
]

Related

PHP json decoder string

anyone can help to decode this?
var_dump
sring(56441) "{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}"
I need to get "data" from this code.
<?php
$data='{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$data=json_decode($data,true);
echo '<pre>';
print_r($data['success']['data']);
It's a simple json that you need to make it an array and access it as a normal array. The output is the data field you asked:
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA
Well, you only need to json_decode() that string and you have your result:
<?php
$input = '{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$output = json_decode($input);
print_r($output->success->data);
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA

Json decode returns null value

i know this question ask many time before but still i could't get this working.
i have a json and when i dump $TenentsAccessible output is this
string(71) "[{`TenantID`:`test.com`,`Name`:`12thdoor`}]"
i need to get the value inside TenantID property. so i use json decode to convert this to php array but is returns null
$jnTenant = json_decode($TenentsAccessible,TRUE);
$tenantID = $jnTenant["TenantID"];
var_dump($jnTenant); // this return null
i try to remove the &quot and unwanted characters using this
$TenentsAccessible = str_replace('"', '"', $TenentsAccessible);
$TenentsAccessible=preg_replace('/\s+/', '',$TenentsAccessible);
i know this type of question ask before but i still could't get this to work. appropriate the hlep. thanks
you can check your json code on JsonLint.
I tried your code and it's not correct because of backticks (`).
So you should replace with (") to have
[{
"TenantID": "test.com",
"Name": "12thdoor"
}]
As hasan described in his answer, json_decode returns a multi-dimensional array, so to get TenantID:
$jnTenant = json_decode('[{"TenantID":"test.com","Name":"12thdoor"}]',true);
$tenantID = $jnTenant[0]['TenantID'];
var_dump($tenantID) ;
If you want to get the "TenantID" in the way you described, you have to modify (if you can) the json like this
{
"TenantID": "test.com",
"Name": "12thdoor"
}
Hope it helps.
try it :
$jnTenant = json_decode('[{"TenantID":"test.com","Name":"12thdoor"}]',true);
$tenantID = $jnTenant[0]['TenantID'];
var_dump($tenantID) ;
correct json and corect get json !
for understand this plz print_r( $jnTenant );
this varibale is Two-dimensional array .

How do I "trim" this JSON with PHP?

I have a JSON that's strangely formatted ...but it's the way I receive it. Because the arrays inside are huge, simply copying and pasting it takes a long time, so I'm wondering if there's a PHP way to do it.
The way I get it is like this:
{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}}
But I want to get only the "results" array, basically the part of [{"title":"Something ....}]. How would I do that?
Do
$arr = json_decode(your_json, true);
If you ned it as JSON again, do
json_encode($arr['results']);
You can get to that part as follows:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}';
$obj = json_decode($json);
echo $obj->results[0]->title;
Outputs:
Something ....
You have to decode your json. Be sure that the json is valid!
Your decoded json returns an object of type stdClass instead of an array!
See below the tested code:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}';
$decoded = json_decode($json);
So you can access array results from the object $decoded:
print_r($decoded->results);
But, in the array, there are objects, thus:
echo $decoded->results[0]->title; // Something ....

How to parse JSON string in php

I'm getting below JSON response:
[{"startDate":"2012-07-12 11:21:38 +0530","totalTime":0},{"startDate":"2012-07-11 11:27:33 +0530","totalTime":0},{"startDate":"2012-07-16 18:38:37 +0530","totalTime":0},{"startDate":"2012-07-17 14:18:32 +0530","totalTime":0}]
i want make array of start date and totalTime, i have used these two lines but it wont work $obj, please suggest..
$obj = json_decode($dateTimeArr);
$dateAr = $obj->{'startDate'};
As everyone said, and you did - use json_decode.
$dateArrays = json_decode($dateTimeArr, true); // decode JSON to associative array
foreach($dateArrays as $dateArr){
echo $dateArr['startDate'];
echo $dateArr['totalTime'];
}
In future, if you are unsure what type or structure of data is in the variable, do var_dump($var) and it will print type of variable and its content.
It is very easy:
$Arr = json_decode($JSON, true);
json_decode() will give you nested PHP types you can then descend to retrieve your data.
use json_decode($json_response,true) to convert json to Array
Guess what you are looking for is json_decode()
Check out http://php.net/manual/en/function.json-decode.php for the inner workings

Reading multiple json values with php

I am trying to read certain values from a json string in php, I am able to do a simple json string with only one value such as
$json = '{"value":"somevalue"}';
Using this:
<?php
$json = '{"value":"somevalue"}';
$obj = json_decode(json_encode($json));
print $obj->{'value'};
?>
But when i try an get a value from the following json string it throws an error...
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
I validated the json on JSONlint but not sure how to access the values within this with php.
Thanks
You can try this:
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
//since $json is a valid json format you needn't encode and decode it again
$obj = json_decode($json);
print_r($obj->filed);
print_r($obj->rule);
You can pass true as a second parameter to json_decode() to get the results as an array
$my_arr = json_decode($json, true);
var_dump($my_arr);
Should help you. You can then step through the array as you would normally.
use var_dump to print out the object with all it's members and hierarchy. you should then be able to find the value you are looking for

Categories