PHP Json Parsing 6 - php

How do I pull the 76561198216468627 from
{ "response": { "steamid": "76561198216468627", "success": 1 } }
I've tried this but it pulls 1 instead of the id
foreach ($parsed_json->{'reponse'} as $item) {
$steamid = $item;
}

You need to decode the JSON string into either a PHP object or array. Here's how to do it each way:
ARRAY METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString, true);
foreach($json as $item)
{
$steamid = $item['steamid'];
}
OBJECT METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString);
foreach($json as $item)
{
$steamid = $item->steamid;
}

Try this:
$json = '{
"response": [{ "steamid": "76561198216468627", "success": 1 }]
}';
$decode = json_decode($json);
echo $decode->response[0]->steamid;
Then you can get 76561198216468627

Related

Insert new response in PHP

I have two json's Response
{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}
Second one response is
{
"balance" : "1000000"
}
I want to merge them and have a json like
{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success",
"Balance" : "1000000"
}
}
Code
$ini = json_encode(array_merge(json_decode($response, true),$b));
the response like this
{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "145541",
"DestinationAcc": "7001520304",
"DestinationName": "NANDA FERNANDO",
"MsgKey": "88dc9a20",
"message": "Inquiry success"
},
"balance": "1000000"
}
Maybe you can get an idea from this.
var json1 = '{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}';
var json2 = '{
"balance" : "1000000"
}';
for (var key in json2) {
json1[key] = json2[key]
}
var response = JSON.stringify(json1);
alert(response);
You need to use json_decode(),array_merge() and then json_encode():
<?php
$json = '{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}';
$json2 = '{
"balance" : "1000000"
}';
$arr1 = json_decode($json, true);
$arr2 = json_decode($json2, true);
$arr1['data'] = array_merge($arr1['data'], $arr2);
$data = json_encode($arr1);
echo $data;
Output: https://3v4l.org/bBiis
I see what you are trying to achieve is already achieved , but you want to show it pretty view ( like you snippet ) right ?
you can use JSON_PRETTY_PRINT for that.
This can be simplified more, but for your better understanding,
here it is.
$json = '{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}';
$json2 = '{
"balance" : "1000000"
}';
$arr1 = json_decode($json, true);
$arr2 = json_decode($json2, true);
$arr = array_merge($arr1, $arr2);
$json_string = json_encode($arr, JSON_PRETTY_PRINT);
echo $json_string;
However JSON isn't supposed to contain HTML line breaks, whereas newline characters are valid in JSON. If you want to display JSON on a web page then do a string replacement on the newline characters yourself or else put the JSON in a < pre > element. See json.org for the syntax reference.

PHP JSON decode without foreach

how can I get the "avatarmedium" from i json without a foreach Loop?
Thats my current Code:
$json = '{
"response": {
"players": [
{
"steamid": "76561198019025166",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d3/d3ca76e0f1e8d071e826bc3baeebd0e7215e50e2_medium.jpg"
}
]
}
}';
$result = json_decode ($json);
echo $json->response[0]->players->profileurl;
Thank you
with best regards
<?php
$json = '{
"response": {
"players": [
{
"steamid": "76561198019025166",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d3/d3ca76e0f1e8d071e826bc3baeebd0e7215e50e2_medium.jpg"
}
]
} }';
$result = json_decode ($json);
echo $result->response->players[0]->avatarmedium;

Php Json Get inside array

This is My json How can get "download" string
{
"total": 1,
"items": [
{
"id": "1186e1472bc1c",
"title": "title,
"desc": "",
"download": "www.google.com",
}
]
}
This is My code:
$response = file_get_contents($nagu);
$obj = json_decode($response, true);
$download = $obj ['items'];
echo $download;
I want output "www.google.com".
$download = $obj ['items'][0]['download'];
echo $download;

Display User data using JSON and PHP

I have a jSON file and I want to display its data but it show me result in some strange format i don't know where I'm wrong. Here is my PHP code,
$json = file_get_contents('data.json');
$data = json_decode($json,true);
$users = $data['user'];
foreach($users as $user)
{
echo $user['user'];
}
Data in my jSON file below,
{
"user":
{
"id":"#79F9FFB1EE0CB1CC",
"user":"test#mail.com",
"password":"123456",
"email":"test#mail.com",
"name":"John Doe",
"creationDate":1387111401
},
"status":
{
"version":"0.9.9.1",
"command":"getuser",
"opf":"json",
"error":false,
"code":0
}
}
It's your foreach loop that is wrong.
You only have on user so replace your loop by :
$users = $data['user'];
echo $users['user'];
$json = '{
"user":
{
"id":"#79F9FFB1EE0CB1CC",
"user":"test#mail.com",
"password":"123456",
"email":"test#mail.com",
"name":"John Doe",
"creationDate":1387111401
},
"status":
{
"version":"0.9.9.1",
"command":"getuser",
"opf":"json",
"error":false,
"code":0
}
}';
$data = json_decode($json,true);
$users = $data['user'];
foreach($users as $key=>$user)
{
echo $user.'<br>';
}

Extracting information from an array in php

Can any one tell me how to extract the 'honda' value out of the array below using php?
{
"version": "1.0",
"encoding": "UTF-8",
"entry": {
"name": "bob",
"car": {
"model": "honda"
}
}
}
This looks like a json encoded object. What you could do is:
$info = json_decode($data, true); //where $data has your stuff from the question
$carModel = $obj['entry']['car']['model'];
If you have all that in a variable called "obj", then
$obj = '{ "version": "1.0", "encoding": "UTF-8", "entry": { "name": "bob", "car": { "model": "honda" } } }';
$arr = json_decode($obj, true);
echo $arr['entry']['car']['model'];
should be 'honda'
EDITED: Per Omar below, you do need the true as the second param in json_decode. He should get selected as the correct answer.
Use json_decode as: http://php.net/manual/fr/function.json-decode.php
<?php
$json = '{"version": "1.0","encoding": "UTF-8","entry": {"name": "bob","car": {"model": "honda"} } }';
$tab = json_decode($json, true);
$honda = $tab['entry']['car']['model'];
var_dump($honda);
// or with object:
$obj = json_decode($json);
$honda = $obj->entry->car->model;
var_dump($honda);

Categories