In my database I have POSTed following JSON with REST API:
{
"author": "Someone",
"hero_name": "Iron Man",
"hero_desc": ["Iron", "Man"],
}
Now in database Table, the entry says only Array for the hero_desc.
I cannot figure out how to get the actual items in that array.. for example to fetch Iron from hero_desc.
Here is how it gets returned with GET request:
{
"id": "11",
"user_id": "1",
"author": "Someone",
"hero_name": "Iron Man",
"hero_desc": "Array",
}
I am quite new to PHP so I am worried that I would need to mess up my whole app architecture to get this to work. I though PHP can handle fetching arrays by default if it sees it's an JSON Array.
use json_decode(); function for change json to array
$json_to_array = json_decode($your_json_variable_name);
You could change array data to JSON with json_encode In one hand you have an array without the key that code makes a number key for that in another hand if have an array with the key you can get that inside JSON with that key.
$content ='your content or your array data';
header('Content-Type: application/json; charset=utf8');
echo json_encode($content);
Related
I am revising my php knowledge. I have external.js file in which there are records in it.
var data = [{
"id": "71002",
"fullName": "Chenz",
"title": "Mechanical Engineer",
"reportTo": "Structural Manager",
"Reportingday": "2017-01-01T09:00:00.000Z" }, .....so on
I need to create html/php page from this record and list all the data above. How will I achieve this thing?
Thank you.
Your Data:
var data = [{"id": "71002","fullName": "Chenz","title": "Mechanical Engineer","reportTo": "Structural Manager","Reportingday": "2017-01-01T09:00:00.000Z" },
Looks to be JSON. You should look at the json_decode() and json_encode() functions. It will convert JSON to an array and an array to JSON. Once in an array you can easily used that to create web pages.
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.json-encode.php
<?php $myData = json_decode('data = [{"id": "71002","fullName": "Chenz","title": "Mechanical Engineer","reportTo": "Structural Manager","Reportingday": "2017-01-01T09:00:00.000Z" }');
Would allow you to call the values something like so
echo '<span>'.$myData->data['id'].'</span>';
Would return
<span>71002</span>
You can test and look at the data once in array format with something like
die(print_r($myData));
from angularjs 2 my application getting below json value but i am not able to convert this json value to an array.
$response = '{username: "mosh", time: "2017-01-22T11:28:54.422Z"}';
$json_arrya = json_decode($response,true);
var_dump($json_arrya);
current output: NULL
expected output as an array
Try the json below
{
"username": "mosh",
"time": "2017-01-22T11:28:54.422Z"
}
to see if that works.
Also, this is a json object not an array so you might have to pass it as an array from your angular service like
[{ "username": "mosh", "time": "2017-01-22T11:28:54.422Z"}]
I'm trying to get specific data from a JSON file (url) and save that data in a PHP variable.
The JSON comes from an url. For example:
https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/Viducius?api_key=secret
The JSON file:
{
"viducius": {
"id": 26541044,
"name": "Viducius",
"profileIconId": 591,
"revisionDate": 1480517139000,
"summonerLevel": 30
}
}
So my question is how can I get the data from id, in this case 26541044, and put that into a PHP variable?
Also to get the data from the JSON file, I have to search for the id while the array name 'viducius' came from a PHP variable.
TLDR:
The array $arrayName contains an id '26541044' that has to be saved into variable $id
Is there someone who can help me with this? I was thinking on doing this in jQuery but if it's easier with PHP only thats fine too.
That is a JSON String, therefore you need to convert it into a PHP data structure. PHP Provides json_decode() to do that.
$json_string = '{"viducius": {
"id": 26541044,
"name": "Viducius",
"profileIconId": 591,
"revisionDate": 1480517139000,
"summonerLevel": 30
}
}';
$obj = json_decode($json_string);
$id = $obj->viducius->id;
echo 'id = ' . $id;
1[[23[]]
I understand that '[' denotes a JOSNArray and '{' denotes a JSONObject. However as my result starts with [ is the entire data piece now an array even if { comes before the actual string?
And if parsing in android, should I be parsing for array or object?
My PHP script:
It's an array contsining single object. The string delimits name of the first item of the first object.
As stated by Zbynek Vyskovsky its an array with single object so to get the data you can use
JSONArray jsonArray=new JSONArray(<your json object>);
JSONObject jsonObject = jsonArray.get(0);
String date = jsonObject.getString("Date");
String temp = jsonObject.getString("Temp");
The best way to determine this is format first .
paste your response in this site http://json.parser.online.fr/
there you will see
1)red square brackets -represent an array.
2)blue curly brackets represents an object.
in short you have an array containing single object.
Your Json is an array which contains only one Json object.I will show actually how it works with JSon Array and JsonObject.
For example I have a JSON Array :
[{
"name": "abc",
"description": "ABC server",
"state": "online"
},
{
"name": "xyz",
"description": "XYZ client",
"state": "online"
},
{
"name": "mnp",
"description": "MNP server",
"state": "online"
}]
So The above array actually contains 3 Json objects like [{Obj1},{Obj2},{Obj3}].
When we try to get any of the value for a key then first we need to store the JSon Array and then after we need to get the exact Json object by passing index e.g: jsonObject obj = JsonArray.get(0).
Soon after you pass the key to the Json object in get() method, you will get your key. e.g: String s = obj.get("state").
{
"Group": [
{
"name": "HolderOne",
"operators": [
{
"username": "ken",
"status": 3
},
.....etc.....
The JSON feed I am attempting to manipulate has to format above.
I wish to be able to display username and status.
$json = file_get_contents("urlhere");
$obj=json_decode($json);
echo $obj->username;
echo $obj->status;
This obviously doesn't work as they are nested(?) within the feed...I have tried:
$obj->Group[0]->name->operators->username
and
$obj->Group[0]->name->username
to no avail (as well as json_decode with ,true and ['name'], etc).
Am I being particularly dim?
when I do a var dump, the data is being collected from the feed okay.
The best way to figure this out is to iteratively do print_r's:
print_r($obj)
//prints what you see above
print_r($obj['Group']
//prints the Group Object
print_r($obj['Group'][0])
//prints first element in Group Object
print_r($obj['Group'][0]['operators'])
//etc.....
That's how I find out how to access these deep elements if I get a little stuck. Though it appears to me that you want:
$obj->Group[0]->operators[0]->username