i need help on parsing json url on php - php

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'];

Related

php - convert mysql data into json object

I using codeigniter. I want to retrive data from database and convert it into JSON object not JSON array.I'm using following code
public function json()
{
$content = $this->db->get('todolist'); //todolist table name
$data = $content->result_array();
echo json_encode($data);
}
Above code is converting database into JSON array.
Output
[{"todo_id":"1","todo_content":"Homework","date":"2016-05-05","iscomplete":null,"imagelink":"Lighthouse.jpg"},{"todo_id":"2","todo_content":"exam","date":"2015-04-21","iscomplete":null,"imagelink":"Desert.jpg"},{"todo_id":"3","todo_content":"Lab report","date":"2014-08-29","iscomplete":null,"imagelink":"FB_IMG_14700753538617403.jpg"}]
What will be best way to convert it into JSON object
Sangam try to understand the concept, check the following line:
$data = $content->result_array(); // $data is an array
echo json_encode($data); // here you are converting it into an json object
Your $data array contains more than one index in it that's why the json object is having multiple {} inside [];
You want to json_encode($data, JSON_FORCE_OBJECT).
The JSON_FORCE_OBJECT flag, as the name implies, forces the json output to be an object, even when it otherwise would normally be represented as an array.
Refer: PHP Array to Json Object
You can use JSON_FORCE_OBJECT see the example below.
echo json_encode($data, JSON_FORCE_OBJECT);
Assuming you're only getting one row back from your query.
change
echo json_encode($data);
to
echo json_encode($data[0]);

How to insert an array in a json object in PHP

I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.

add new field and data with php to json encoded object with quotes removed

I converted php array to json and I try to add another field and data with quotes removed.
this is json object generated
$data_string = json_encode($data);
It outputs this .
{"dateDebut":"36000000","dateFin":"45000000","periodeDebut":"1410818400","periodeFin":"1411596000","jours":"Thursday","role":{"idRole":"1"},"zone":{"idzone":"Z1E2"},"tag":{"id":"511651969251"},"typeNotification":{"typeNotif":"Alerte"}}
I tried this
$data_string['message']=1;
and it outputs this wrong object with the "1" in the beginning
1"dateDebut":"36000000","dateFin":"45000000","periodeDebut":"1410818400","periodeFin":"1411596000","jours":"Thursday","role":{"idRole":"1"},"zone":{"idzone":"Z1E2"},"tag":{"id":"511651969251"},"typeNotification":{"typeNotif":"Alerte"}}
even adding the field with quotes like this
$data_string['message']="1";
doesn't add the field message in the generated object json at all.
You cant add data to the json string, because its a string.
Add the data before you json encode it:
$data['message']=1;
$data_string = json_encode($data);
Or if the original php object $data is out of scope by this point, you must decode to php object, add data and then encode back to json:
$data = json_decode($data_string);
$data['message']=1;
$data_string = json_encode($data);
YOu can do this:
$data_array = json_decode($data_string);
$data_array['message'] = 1;
$data_string = json_encode($data_array);
The string $data_string should contain the new member message with value 1;

How to take particular value from json response in php

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'];

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