My Json String looks like this:
Now i wanted to take only value of ID as an Integer:
[{"Obj" :
{ "ID":"11",
"NAME":"XYZ",
"GENDER":"M"
}
}]
How can i do this?
Try this,
<?php
$json='[{"Obj" :
{ "ID":"11",
"NAME":"XYZ",
"GENDER":"M"
}
}]';
$jsonArray=json_decode($json);
echo $jsonArray[0]->Obj->ID;
?>
assuming that your json string is inside a post parameter:
$json_string = $_POST['json'];
using json_decode you can convert a json string to a php object:
$json = json_decode($json_string);
and then access to your ID:
$id = $json[0]->Obj->ID;
if you want to convert the object into associative array just do this:
$json = (array)$json;
and access to your id:
$id = $json[0]['Obj']['ID'];
Decode the json output using json_decode and put true as second parameter. It will give you array output.
$arr = json_decode($json,true);
echo $arr[0]['Obj']['ID'];
Related
Here is the json content:
{
"success":true,
"data":"{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"}",
"message":null
}
I'm looking to get the value of download_link. How can I do that?
This is what I tried:
<?php
$jsndata = {"success":true,"data":"{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"}","message":null};
$jsn = json_decode($jsndata,true);
$temperatureMin = $jsn['data'][6]['download_link'];
echo $temperatureMin;
?>
You have a json object containing another serialized json object.
Decode the first object, get the second serialized object and decode that:
$jsndata = '{
"success":true,
"data":"{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"}",
"message":null
}';
// Decode the main json object
$jsn = json_decode($jsndata,true);
// Since 'data' is another serialized object, you need to decode that as well:
$data = json_decode($jsn['data'], true);
// Now you can access the contents of 'data'
echo $data['download_link'];
Demo: https://3v4l.org/1PcQp
You need:
- quotes around your JSON
- to remove the quotes around the inner object
$jsndata = '{"success":true,"data":{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"},"message":null}' ;
And you need to remove the [6] from your array access:
$temperatureMin = $jsn['data']['download_link'];
I want to get the value of PARAMS in my url
app-rolly.lan.beestripe.privsub.net/t/e/?callback=callback&aid=13&alias=PxTRvtD9vCJN0VpmgQlNhtR1d5Ec&ua=chrome-Windows&event=result-click-ad&cb=1476946609765¶ms=%7B"search_source"%3A"toolbar"%2C"test_id"%3A2%7D
Those all json data you just decode it into array using json_decode()
$ARRAY = json_decode($result,true);
you will get the array value you can access like this
echo $ARRAY['search_source'];
Example :
<?php
$result = '{"search_source":"toolbar","test_id":2}';
$ARRAY = json_decode($result,true);
echo $ARRAY['search_source'];
?>
OUTPUT :
toolbar
I'm working with Laravel 5 right now and I have the following problem. I've got response from DB query:
[{"id":1}]
and I want to take out 1 as int or string. Any ideas?
I've tried to solve this like follows:
$json = (DB query);
$data = json_decode($json);
$final = $data[0]->id;
and response is :
json_decode() expects parameter 1 to be string, array given
This is all you need.
$response = json_decode($response); // Decode the JSON
$string = $response[0]->id; // Save the value of id var
As you say, the string you have is in JSON format, so you need to use json_decode to access it in PHP.
The square brackets refer to an array, and the braces refer to an object within that array, so what you're looking for is the id value of the first element (i.e. element 0) in the array.
<?php
$json = '[{"id":1}]';
$data = json_decode($json);
echo $data[0]->id;
// 1
Try this
$json_array = '[{"id":1}]';
$data = json_decode($json_array);
print_r($data); // To display the result array
You just need to decode it, if I'm not misunderstanding your questions.
$json = '[{"id":1}]';
$decodedObject = json_decode($json);
Then you can loop through the aray and do something with your data:
foreach($decodedObject as $key => $value) {
$id = $value->id;
}
If you're using Laravel, though, why not use Eloquent models?
i'm tring to parse url's json string to an json array.
ISSUE: json_decode = empty
QUESTION: Does anyone see what am i doing wrong?
MY STEPS:
tests from browser:
.....send.php?{"contactName":"name1"},{"contactName":"name2"}
my php code:
1. $url = $_SERVER['QUERY_STRING'];
2. $urlStringDecoded = urldecode($url);
echo urlStringDecoded result ok:
{"contactName":"name1"},{"contactName":"name2"}
3. $json = json_decode($urlStringDecoded, true);
RESULT EMPTY
echo("$json");
Seems like your JSON is invalid. Wrap your current string within [ ] Brackets.
Eg.
<?php
$url = $_SERVER['QUERY_STRING'];
//echo $url;
$urlStringDecoded = urldecode($url);
echo $urlStringDecoded;
$json = json_decode("[".$urlStringDecoded."]", true);
echo "<pre>";
print_r($json);
echo "</pre>";
?>
As you are trying to convert json string to json, you should provide right format. From you example it looks like you are passing , comma separated json objects whereas it should be an array. Add [] around your string and then try, in other words make it an array.
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