getting error while converting json arry - php

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"}]

Related

PHP Arrays. Trouble formatting result array

I am working on an API in PHP that sends JSON data in form of an array to an android app. I'm having issues in returning the JSON array in the format;
"sammury" : [
{ "ClientName": "Samuel", "OrdersTaken": 400 },
{ "ClientName": "James", "OrdersTaken": 20 },
{ "ClientName": "Ritah", "OrdersTaken": 9 }
]
I'm using a php server online and a mysql database. I have added an image showing how the data that is returned loks like. Any Assistance Will be highly appreciated
Here is a simple example for you how to send your desired response.
1) Create your object arrays
2) Push the objects into the data array (you can use loop to push objects to array) and then assign that data array to the response array and convert it to Json.
Please see the working code here
$data = [];
$obj = [
"ClientName"=> "Samuel",
"OrdersTaken"=> 400
];
array_push($data, $obj);
$response = [
"sammury"=> $data
];
$json = json_encode($response);
echo $json;

How to correctly parse JSON in PHP

I want to parse values from an Json API, but I cant get it to work
The API returns this JSON:
[
{
"assets": [
{
"id": 6,
"size": 1429504,
"download_count": 1,
"browser_download_url": "https://dl.domain.tld/files/cdbc6e19-cd86-4ed6-8897-37ec5aaee578"
}
]
}
]
I tried to get the ID value like this:
$json_obj = json_decode($resp);
print $json_obj->assets[0]->id;
but I get no result whereas it should be 6. What do I do wrong here?
Remember the outer part of the JSON is an array, as suggested by the opening [. So you need to first access the first (and only) element of it:
$json_obj[0]->assets[0]->id; //<-- note the first [0]
I think the correct answer is
$json_obj = json_decode($resp);
print $json_obj[0]->assets[0]->id;
The json object will be converted to a php array, since you have an array with a object inside in your case it will be a multidimentional array with the objects inside.
Try this its worked for me..
$json ='[
{
"assets": [
{
"id": 6,
"size": 1429504,
"download_count": 1,
"browser_download_url": "https://dl.domain.tld/files/cdbc6e19-cd86-4ed6-8897-37ec5aaee578"
}
]
}
]';
$json_obj = json_decode($json);
var_dump($json_obj[0]->assets[0]->id)
?>
decode JSON to the array and fetch the id by proper array keys
$jToArray = json_decode($resp, TRUE);
echo $jToArray[0]['assets'][0]['id'];//You will get the correct id

How to fetch an Array from MySQL with PHP

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);

Is my JSON an array or an object?

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").

Parse json array without value in php

I wonder how to parse a json array without values
Json: {"status":"FAILED","errors":{"email":["NOT_UNIQUE"],"name":["TOO_SHORT"]}}
How can i get the value of email in a foreach loop?
What i mean with "without value" is: there is an array called email and name... How can i get the value for "email" that currently says NOT_UNIQUE?
In your current example, your JSON string is malformed. I dont know if thats a typo on your part while creating your question. Assuming the JSON string is okay in your code, a simple json_decode() will do just fine. Consider this example:
$json_string = '{ "Json": { "status": "FAILED", "errors": { "email": [ "NOT_UNIQUE" ], "name": [ "TOO_SHORT" ] } }}';
$data = json_decode($json_string, true);
echo $data['Json']['errors']['email'][0]; // NOT UNIQUE
use json_decode, json_decode($str, true) will return it as an assosiative array whereas json_decode($str, false) will return objects.
json_decode("{"status":"FAILED","errors":{"email":["NOT_UNIQUE"],"name":["TOO_SHORT"]}}", true)['errors']['email']
should get the email for you.

Categories