{
"response": {
"request": {
"date": "Tue, 13 Sep 2011 11:24:28 +0200",
"resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
"status": {
"flag": "success",
"code": 200
}
},
"result": {
"data": {
"user": {
"user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
"date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
"date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
"num_logins": "64",
"identities": [{
"identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
"provider": "twitter",
"id": "http://twitter.com/ExampleUser",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname"
},
"gender": "male",
"utcOffset": "2:00"
}, {
"identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
"provider": "facebook",
"id": "http://www.facebook.com/profile.php?id=1046121518",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname",
"givenName": "Firstname",
"familyName": "Lastname"
},
"gender": "male",
"birthday": "01/01/1980",
"utcOffset": "2:00",
"emails": [{
"value": "email#example.com",
"is_verified": "true"
}]
}]
}
}
}
}
}
The above code is Result: the code returned by the ONEALL API
I am using oneall api for my website social login ,after i login i requested user details by get method using user token, the values like last login got by using $data->user->date_last_login, but i am unable to get email and dispalyname how can i do that one , i didn't wrote all the code i gave just sample, hope this will help for others also
http://docs.oneall.com/api/resources/users/read-user-details/
If you follow the object structure, you can get them as:
$data = json_decode($json);
$displayName = $data->response->result->data->user->identities[1]->displayName;
$email = $data->response->result->data->user->identities[1]->emails[0]->value;
Update:
If you are using intermediate var for response data, then just shrink the chain:
$json = json_decode($result_json);
//extract response data
$data = $json->response->result->data;
$displayName = $data->user->identities[1]->displayName;
$email = $data->user->identities[1]->emails[0]->value;
This will give you:
Firstname Lastname
email#example.com
Here we go complete code, based on your JSON object:
<?php
$result_json = '
{
"response": {
"request": {
"date": "Tue, 13 Sep 2011 11:24:28 +0200",
"resource": "/users/2cec711d-ca14-4472-98c8-ca74432bc2d3.json",
"status": {
"flag": "success",
"code": 200
}
},
"result": {
"data": {
"user": {
"user_token": "2cec711d-ca14-4472-98c8-ca74432bc2d3",
"date_creation": "Tue, 1 Sep 2011 11:01:12 +0200",
"date_last_login": "Tue, 13 Sep 2011 01:05:07 +0200",
"num_logins": "64",
"identities": [{
"identity_token": "cd3bd13b-b393-4d6c-a7f6-950b4c47938f",
"provider": "twitter",
"id": "http://twitter.com/ExampleUser",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname"
},
"gender": "male",
"utcOffset": "2:00"
}, {
"identity_token": "3ab5257b-ba2b-4242-a7f6-950b4c47938f",
"provider": "facebook",
"id": "http://www.facebook.com/profile.php?id=1046121518",
"displayName": "Firstname Lastname",
"name": {
"formatted": "Firstname Lastname",
"givenName": "Firstname",
"familyName": "Lastname"
},
"gender": "male",
"birthday": "01/01/1980",
"utcOffset": "2:00",
"emails": [{
"value": "email#example.com",
"is_verified": "true"
}]
}]
}
}
}
}
}';
$json = json_decode($result_json);
$data = $json->response->result->data;
echo $displayName = $data->user->identities[1]->displayName;
echo '<br>';
echo $email = $data->user->identities[1]->emails[0]->value;
?>
Related
In my JsonArray result, I want to put all the content in index [{'data':{contenthere}}]
Here is my code :
$data = [];
$gr = []; //some data here
foreach($gr as $g) {
$data[] = [
'id' => $g['id'],
'name' => $g['name'],
'phone' => $g['pĥone']
]
}
return $data;
return $data output :
string(249) "[
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
},
]
cool, now I want to put all this in ['data'] so the result needed :
string(249) "[
{
"data": {
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
},
}
}
]
As Google JSON Style
I tried the $data[]['data'] but the "data": repeats in each object
foreach($gr as $g) {
$data[]['data'] = ...
I thought do do group_by function but I think that there is a simple solution for that
I'm not sure if you really need the extra array at the top level, but use...
return [["data" => $data]];
should give you the result.
Use json_encode to achieve this:
$json = json_encode(array('data' => $data));
print_r($json);
Result:
{
"data": [
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
}
]
}
this is json response:
{
"level1": {
"level2":
{
"name": "abc",
"age": "19",
"email": "abc#g.com",
"gender": "Female",
"dob": "7 june 1999",
"add": "sdasdadas"
}
}
}
I want to convert JSON response as following way:
{
"level1": {
"level2":
[{
"name": "abc",
"age": "19",
"email": "abc#g.com",
"gender": "Female",
"dob": "7 june 1999",
"add": "sdasdadas"
}]
}
}
I converted my XML response into JSON form and get the object if I have one user details in level2 but I want it in an array in level 2 even have multiple user details or single user details..how can be possible?
Please try this code as #simonecosci mentioned in the comment.
$responseData ='{
"level1": {
"level2":
{
"name": "abc",
"age": "19",
"email": "abc#g.com",
"gender": "Female",
"dob": "7 june 1999",
"add": "sdasdadas"
}
}
}';
$json = json_decode($responseData);
$json->level1->level2 = [$json->level1->level2];
echo '<pre>';
print_r(json_encode($json));
echo '</pre>';
Expected output:
{"level1":{"level2":[{"name":"abc","age":"19","email":"abc#g.com","gender":"Female","dob":"7
june 1999","add":"sdasdadas"}]}}
I'm having an issue where my android application is getting a positive result from our key:answer POST, but not from key:array for the same structure. My web developer is saying that I'm not serialising it properly, but every JSON tester I can find on the web is quite happy with the send, just not our web server. I'm sending my JSON in groups of 10 as a string (We had a previous issue where when sending about 200 keys it was giving issues, so I've split it down to 10 per send) and the JSON sent is as follows:
[{
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "date_inspDate",
"answervalues": "18-07-2017"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_clientRep",
"answervalues": "testrep"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_designCode",
"answervalues": "destdesign"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_qcPlan",
"answervalues": "testqc"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_equipDesc",
"answervalues": "testdesc"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_companyName",
"answervalues": "testco"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_drawingNo",
"answervalues": "testrev"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Detail",
"answervalues": [{
"register": 1,
"values": "act2"
}, {
"register": 2,
"values": "act3"
}]
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Regulated",
"answervalues": [{
"register": 1,
"values": "YES"
}, {
"register": 2,
"values": "NO"
}]
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Comments",
"answervalues": [{
"register": 1,
"values": "DIMENSIONS CHECK"
}, {
"register": 2,
"values": "HOLD POINT"
}]
}]
All is fine until it hits the Detail key, which is an array. This structure was sending fine last week, they claim they've changed nothing. Is it really a serialisation issue on my side or is the server just not accepting that key as either value or array?
From your error message:
"JSON parse error: Can not deserialize instance of java.lang.String out of START_ARRAY token;"
I think you problem is in 'answervalues'.
/* in some entries you have it as an array of objects*/
"answervalues": [{
"register": 1,
"values": "YES"
}, {
"register": 2,
"values": "NO"
}]
/* while in other entries as a string */
"answervalues": "testrev"
Talk with your developer to see how this need to be formatted. I believe the string values also need to be passed as an object in a single array. Something like..
"answervalues": [{
"register": 1,
"values":"testrev"
}]
I need your help..I would like to call a json value using a json value...
here's my sample json...
first json:
{
"first_name": "Richard Catibog",
"last_name": "",
"display_name": "Richard Catibog ",
"roles": {
"owner": false,
"administrator": true
},
"login_count": 108,
"enabled": true,
"departments": [
1817719575,
1817719887,
1817722991
],
"id": 4142095,
"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
},
{
"first_name": "Rovi Cruz",
"last_name": "",
"display_name": "Rovi Roy Cruz ",
"roles": {
"owner": false,
"administrator": true
},
"login_count": 98,
"enabled": true,
"departments": [
1817719575,
1817719887,
1817722991
],
"id": 4225009,
"last_login": "2017-07-13T00:37:27Z",
"create_date": "2017-06-05T06:15:49Z",
"email": "rovi.cruz+21821290968#51talk.com"
}
Second json:
[
{
"description": "Fees Support for American Academy",
"settings": {
},
"enabled": false,
"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {
},
"enabled": true,
"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {
},
"enabled": true,
"id": 1817719887,
"name": "Technical Support"
}
]
I would like to call the id on the second and echo the names using the departments id list on first json...
here's what i get...
but the result that i want to see is AA Fees Support [L2], Lesson and Fee Support and Technical Support...
thank you, hope you can help me...
In this I made an array $desc that is keyed on the departments id numbers so you can easily convert the id to a Descrition
$js1 = '{
"first_name": "Richard Catibog", "last_name": "",
"display_name": "Richard Catibog ",
"roles": { "owner": false,"administrator": true},
"login_count": 108,"enabled": true,
"departments": [1817719575,1817719887,1817722991],
"id": 4142095,"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
}';
$js2 = '[
{
"description": "Fees Support for American Academy",
"settings": {},"enabled": false,"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {},"enabled": true,"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {},"enabled": true,"id": 1817719887,
"name": "Technical Support"
}
]';
$first = json_decode($js1);
//print_r($first);
$second = json_decode($js2);
//print_r($second);
// make array key'd on the id
$desc = [];
foreach ($second as $s) {
$desc[$s->id] = $s;
}
print_r($desc);
// go through all the dept id's in the first array
// getting a matching desctipyion from array 2
foreach ( $first->departments as $dept) {
echo $desc[$dept]->description . PHP_EOL;
}
UPDATE: After the first json data structure got changed to an array of objects
NOTE I changed your first json data structure to make it valid JSON by adding [] around the 2 objects to make a valid JSON data structure i.e. an array of objects.
All you need to do in this case is add a loop to lop over the now array of objects.
$js1 = '[{
"first_name": "Richard Catibog",
"last_name": "",
"display_name": "Richard Catibog ",
"roles": { "owner": false, "administrator": true },
"login_count": 108,
"enabled": true,
"departments": [ 1817719575, 1817719887, 1817722991 ],
"id": 4142095,
"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
},
{
"first_name": "Rovi Cruz",
"last_name": "",
"display_name": "Rovi Roy Cruz ",
"roles": { "owner": false, "administrator": true },
"login_count": 98,
"enabled": true,
"departments": [ 1817719575, 1817719887, 1817722991 ],
"id": 4225009,
"last_login": "2017-07-13T00:37:27Z",
"create_date": "2017-06-05T06:15:49Z",
"email": "rovi.cruz+21821290968#51talk.com"
}]';
$js2 = '[
{
"description": "Fees Support for American Academy",
"settings": {},"enabled": false,"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {},"enabled": true,"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {},"enabled": true,"id": 1817719887,
"name": "Technical Support"
}
]';
$first = json_decode($js1);
//print_r($first);
$second = json_decode($js2);
//print_r($second);
// make array key'd on the id
$desc = [];
foreach ($second as $s) {
$desc[$s->id] = $s;
}
//print_r($desc);
//Loop through all the first array of object
foreach ( $first as $f) {
// go through all the dept id's in the first array
// getting a matching desctipyion from array 2
foreach ( $f->departments as $dept) {
echo $desc[$dept]->description . PHP_EOL;
}
echo PHP_EOL;
}
RESULT
Fees Support for American Academy
Technical Support
American Academy
Fees Support for American Academy
Technical Support
American Academy
here you go
$json1 = '{
"first_name": "Richard Catibog",
"last_name": "",
"display_name": "Richard Catibog ",
"roles": {
"owner": false,
"administrator": true
},
"login_count": 108,
"enabled": true,
"departments": [
1817719575,
1817719887,
1817722991
],
"id": 4142095,
"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
}
';
$json2 ='[
{
"description": "Fees Support for American Academy",
"settings": {
},
"enabled": false,
"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {
},
"enabled": true,
"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {
},
"enabled": true,
"id": 1817719887,
"name": "Technical Support"
}
]';
$json1 = json_decode($json1, true);
$json2 = json_decode($json2, true);
$department = function($id) use($json2) {
foreach($json2 as $dep) {
if($dep["id"] == $id) {
return $dep;
}
}
return false;
};
foreach($json1["departments"] as $id) {
$dep = $department($id);
if(is_array($dep)) {
echo sprintf("id: %s, name: %s\n", $id, $dep["name"]);
}
}
I am building an admin backend for an android app. My code provides a json output which the android developer then uses in his app. The developer asked me if it is possible to reduce the hierarchy level in the json output
as in remove the highlighted []0 and put the contents directly inside the []data instead.
This is my current php code
if($type == 1 ) //Handle item display
{
try
{
$query = "SELECT category FROM category";
$result= $DBH->query($query);
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$cat = $row['category'];
$query1 = "SELECT * FROM item WHERE catagory='$cat'";
$value = $DBH->query($query1);
if($row1 = $value->fetchAll(PDO::FETCH_OBJ)){
$main[] = array('data'=>array($row1));
}
else
{
$main[] = array('data'=>array('catagory'=>$row['category']));
}
}
echo json_encode($main);
$result->closeCursor(); //Close database connection free resources
$DBH = null;
}
catch(PDOException $e){
print $e->getMessage ();
die();
}
}
And the json output being generated
[
{
"data": [
[
{
"id": "2",
"name": "rice",
"price": "20",
"description": "Plain Rice",
"image": "4106_rice.jpg",
"time": "12 mins",
"catagory": "Lunch",
"subcat": ""
},
{
"id": "3",
"name": "item 1",
"price": "32",
"description": "item 1 description",
"image": "1370_2Ckjikljklkljh.jpg",
"time": "10",
"catagory": "Lunch",
"subcat": "Chicken Soup"
},
{
"id": "4",
"name": "hello",
"price": "10",
"description": "fgsdjfsfsdj",
"image": "",
"time": "76",
"catagory": "Lunch",
"subcat": ""
}
]
]
},
{
"data": {
"catagory": "Dinner"
}
},
{
"data": {
"catagory": "Soup"
}
},
{
"data": {
"catagory": "Test"
}
}
]
I am not very sure if I can make the changes he asked or if it is possible. Is it doable?
Your json structure is inconsistent and try this approach, will be easier for the developer to parse
{
"response": [
{
"data": [
{
"id": "2",
"name": "rice",
"price": "20",
"description": "Plain Rice",
"image": "4106_rice.jpg",
"time": "12 mins",
"catagory": "Lunch",
"subcat": ""
},
{
"id": "3",
"name": "item 1",
"price": "32",
"description": "item 1 description",
"image": "1370_2Ckjikljklkljh.jpg",
"time": "10",
"catagory": "Lunch",
"subcat": "Chicken Soup"
},
{
"id": "4",
"name": "hello",
"price": "10",
"description": "fgsdjfsfsdj",
"image": "",
"time": "76",
"catagory": "Lunch",
"subcat": ""
}
]
},
{
"data": [
{
"catagory": "Dinner"
}
]
},
{
"data": [
{
"catagory": "Soup"
}
]
},
{
"data": [
{
"catagory": "Test"
}
]
}
]
}