How to print a String from a json file in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've a problem.
I want to print out a string the string is in this JSON file with a php scipt:
http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy
it's: data / description
i hope you understand what i mean.

Use jsondecode php function.
$url = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$jsondata = file_get_contents($url); //json data from url
$data = json_decode($jsondata,1); // convert json data to array
echo $data['data']['description']; // access data using keys of array, mapped to properties in json

using json_decode
$json_uri = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$json = file_get_contents($json_uri);
$data = json_decode($json,1); // convert json data to array
$count = 1;
foreach ($data['data'] as $key => $info) {
echo $count++.' - '.$info['description'].'<br>';
}

Related

Access property of a PHP object [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm passing in an object using an ajax function. The object looks like this:
{"label":"1","number":2}
Once the object reaches the server I'm using PHP to json_decode it.
After it's decoded how can I start accessing the properties of the object? For example I want to retrieve the value of label - how can this be done?
$myobj = json_decode($JSON);
print $myojb->label;
You might want to read about Classes and Objects in the manual. An alternative syntax would be decoding the JSON to an array and then accessing it via the key.
$myarr = json_decode($JSON, TRUE);
print $myarr['label'];
You can access it like this;
$json = json_decode($input);
echo $json->label;
$input=json_decode(your json);
echo $input->label;
you can do it this way....
from jquery..
var a= {'label':'1','number':'2'};
$.ajax({
data : {data:JSON.stringify(a)},
})
from php side
if(isset($_POST['data']))
{
$data = $_POST['data'];
$data = json_decode($data,true);
echo $data['label']; //will print label here
}
this is full example of ajax request with json to php...
might help you...

i need Apostrophe in a variable result MYSQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I receive data in this format like:
C00001,C00302,C00303,C00287,C00286,C00285,C00017
in a variable but i need in this shape:
'C00001','C00302','C00303','C00287','C00286','C00285','C00017'
i am new in mysql kindly any help
You can explode the data, modify it, then re-implode it.
$data = 'C00001,C00302,C00303,C00287,C00286,C00285,C00017';
$arr = explode(',', $data);
$new_arr = array_map(function($a){
return "'{$a}'";
}, $arr);
$new_data = implode(',', $new_arr);

Getting value from JSON [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am struggling to get pull the correct values
I am trying to get the following
packages>items then loop through the ids
{"id":1,"title":"Barnsley","slug":"barnsley","packages":[{"id":1,"title":"Group PACKAGE 1","items"
{"id":9,"title":"Guiness","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":10,"title":"John Smith","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":22,"title":"Chicken Cheese and Bacon Melt","band_a":4.5,"band_b":4.75,"band_c":5.0,"band_d":5.25,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null}],"additional_items":null,"event_types":null}]}
Your JSON isn't valid. You're missing two characters :[ that declare the items array. After fixing the JSON (check it on jsonlint.com) you can iterate over the item IDs like so:
$obj = json_decode($str);
foreach($obj->packages[0]->items as $item)
{
echo $item->id;
}
If there could be more than one package you can iterate those too:
foreach($obj->packages as $package)
{
foreach($package->items as $item)
{
echo $item->id;
}
}

How to convert te JSON string to a JSON object in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I got a object like this:
{"1":"FFS","2":"S"}, and I use json_decode function to convert it into an array, but I am not success with it. It becomes the array like this: {"FFS", "S"}, but missing the {"1", "2"}, Can I convert it to become a dict or something, that I can access both value? Thanks.
use true param, to convert object to associative array for json_decode(), like do:
$str = '{"1":"FFS","2":"S"}';
echo "<pre>"; print_r(json_decode($str, true));
gives::
Array
(
[1] => FFS
[2] => S
)
$myjsonobject = json_decode('{"1":"FFS","2":"S"}');
Should working.
Try: print_r($myjsonobject); to validate.
Its working.

Parsing XML from a Web Page with PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I heard that you can parse information from a web page with SimpleXML.
How can I get the the Usernames and ID's from the following page and display them on my website like:
Username:Napolien
Id:2202591
The URL to the website from which I want to get the information is:
http://api.roblox.com/users/3/friends
Your api returns json, you can decode it with json_decode, i have also include how to traverse through the json objects included in the feed.
$json = file_get_contents("http://api.roblox.com/users/3/friends");
$obj = json_decode($json);
for ($i=0; $i < count($obj); $i++) {
echo $obj[$i]->Id;
echo $obj[$i]->Username;
}
The data is actually JSON. You can get it like this :
$jsonString = file_get_content('http://api.roblox.com/users/3/friends');
$data = json_decode($jsonString, true);
$username = $data[14]['username']; // 14 is your desired index
$id = $data[14]['id'];

Categories