Read JSON Data Using PHP [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 months ago.
Solr returns response in following JSON format.
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"indent":"on",
"start":"0",
"q":"*:*",
"wt":"json",
"version":"2.2",
"rows":"10"}},
"response":{"numFound":3,"start":0,"docs":[
{
"student_id":"AB1001",
"student_name":[
"John"]
},
{
"student_id":"AB1002",
"student_name":[
"Joe"]
},
{
"student_id":"AB1003",
"student_name":[
"Lorem"]
}]
}}
What will be the simple way to read student_id, student_name using PHP?

Use $obj = json_decode($yourJSONString); to convert it to an object.
Then use foreach($obj->response->docs as $doc) to iterate over the "docs".
You can then access the fields using $doc->student_id and $doc->student_name[0].

PHP has a json_decode function that will allow you to turn a JSON string into an array:
$array = json_decode($json_string, true);
$student_id = $array['response']['docs'][0]['student_id'];
...
Of course, you might want to iterate through the list of students instead of accessing index 0.

$result = json_decode($result, true);
$result['response']['docs'][0]['student_id'] ...

Why not just use one of the PHP clients for Solr, or the PHP response writer? See http://wiki.apache.org/solr/SolPHP

$json_a = json_decode($string, TRUE);
$json_o = json_decode($string);
#array method
foreach($json_a['response']['docs'] as $students)
{
echo $students['student_id']." name is ".$students['student_name'][0];
echo "<br>";
}
#Object Method`enter code here`
foreach($json_o->response->docs as $sthudent_o)
{
echo $sthudent_o->student_id. " name is ".$sthudent_o->student_name[0];
echo "<br>";
}

Related

how to get element from json_encode array response - Codeigniter [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have a response from my code which looks like this
Controller
$results = $response
echo $result->item;
exit;
Response
{"item":"Samsung A","location":"Hamburg Avenue 5920"}
I want to get only item from the response..How do i achieve this ? The response is json encoded
You need to convert json to object, and after that you can get the element:
$respObject = json_decode($response);
echo $respObject->item;
exit;
json_decode function will helps.
$json_data = '{"item":"Samsung A","location":"Hamburg Avenue 5920"}';
$result = json_decode($json_data);
echo $result->item;
Use json_decode the result will converted into array
$results = json_decode($response);
print_r( $results);
exit;

How can I read this nested json in php? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I have this JSON
{
"AED_USD": {
"2019-08-01": 0.272242
}
}
When dumping this as an array I have:
array(1) { ["AED_USD"]=> array(1) { ["2019-08-01"]=> float(0.272242) } }
I am trying to reach get the float value of 0.272242.
How do I get this value in PHP?
You have to json_decode the string with true as second parameter to make it an array, and then just echo what you want using the name of the keys you have. Like so:
$string = '{
"AED_USD": {
"2019-08-01": 0.272242
}
}';
$decode = json_decode($string, true);
echo $decode['AED_USD']['2019-08-01'];
To get the value in PHP, you can do
$value = $yourArray['AED_USD']['2019-08-01'];
If you want more information: https://www.php.net/manual/pt_BR/language.types.array.php

How to access object data inside an array and object after json_encode executed? [duplicate]

This question already has answers here:
Get value from JSON array in PHP
(4 answers)
Closed 6 years ago.
I have a json data from API and i want to insert them to my database table. I have extract $data using json_encode function, but when i tried to access data inside $myJson with some of this code, it gives me an error result.
$data = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$myJson = json_decode($data);
foreach ($myJson as $mj){
echo $mj['math_score'];
// echo $mj->math_score; <= error
// echo $mj[0]->post->math_score; <= error
// echo $mj->post->math_score; <= error
}
The error : Invalid argument supplied for foreach().
sorry for my bad grammar, any answer will be greatly appreciated. Thanks
There is built in php function json_decode()
Try
$json = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$json = json_decode($json);
echo $json->posts{0}->post->math_score;
By default json_decode return object. If you want an array then you need to pass second argument true to json_decode.
Try it with Array
$json = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$json = json_decode($json, true);
foreach ($json['posts'] as $mj)
{
echo $mj['post']['math_score'];
}

Create array with only tag - Json decode [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
How do I return only all the urls item "webformatURL" using json decode?
{
"totalHits":500,
"hits":[
{
"previewHeight":84,
"likes":37,
"favorites":42,
"tags":"yellow, natural, flower",
"webformatHeight":360,
"views":11218,
"webformatWidth":640,
"previewWidth":150,
"comments":8,
"downloads":6502,
"pageURL":"https://example.com/en/yellow-natural-flower-715540/",
"previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
"webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
"imageWidth":3020,
"user_id":916237,
"user":"916237",
"type":"photo",
"id":715540,
"userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
"imageHeight":1703
},
],
"total":7839
}
I try:
$test= json_decode($json);
$result= $test->webformatURL;
Error: warning-undefined-property-stdclass::webformatURL
I've read the manual but I doubt then I would see an example in practice
json_decode
Thanks for any help
Did you mean?
$result = $test->hits[0]->webformatURL;
If you want to extract only this field from the object, you can do:
$urls = [];
foreach($test->hits as $hit) {
$urls[] = $hit->webformatURL;
}
var_dupm($urls);

Parsing a JSON array of arrays using PHP [duplicate]

This question already has answers here:
how to parse json into php
(5 answers)
Closed 8 years ago.
I know this is rather simple but I'm stuck at it and I'd really like some help
Here's a JSON string I'm generating.
[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]
How do I extract the value associated with field1?
And how do I access elements of each of the distinct arrays?
try this code
$jsonString = '[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]';
$json = json_decode($jsonString, true);
print_r($json);
Try this (in Javascript)
var jsonData=[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}];
if you get response as text then use jsonData=JSON.parse(yourResponseText);
for(var i=0;i<jsonData.length;i++){
alert('your required val:'+jsonData[i].field1);
}
In php
$data = json_decode($json);//$json is your json data
foreach ($data as $item) {
echo $item->field1
}
https://stackoverflow.com/a/3627901/485790
console.log(jQuery.parseJSON(' [{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]').field1);
In php way
$jsonString = '[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]';
$json = json_decode($jsonString, true);
foreach($json as $item){
echo $item['field1'];
}
In php you can:
$json = json_decode($_GET['variable'];
http://php.net/manual/en/function.json-decode.php
try this,
var json = '[{"field1":3,"field2":"5","field3":"value3","field4":"value4"},{"field1":3,"field2":"8","field3":"value3","field4":"value4"},{"field1":3,"field2":"6","field3":"value3"}]';
$.each(jQuery.parseJSON(json), function () {
alert(this['field1']);
alert(this['field2']);
alert(this['field3']);
alert(this['field4']);
});
this is a simple array of json objects. so with jquery
var jsonArray = JSON.parse("your string");
for(var int i=0 ; i < jsonArray.length() ; i++){
var jsonObject = jsonArray[i];
Console.log(jsonObject.field1)
}

Categories