JSON and PHP - Help!

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
codex561
Forum Newbie
Posts: 4
Joined: Tue May 13, 2014 9:54 pm

JSON and PHP - Help!

Post by codex561 »

Hello, I am making a bit of a project and I need help.
I got this JSON thing:
{"character_list":[{"name":{"first":"Dreadnaut","first_lower":"dreadnaut"},"battle_rank":{"percent_to_next":"0","value":"100"}}],"returned":1,"timing":{"character-ms":4,"total-ms":4}}
And I want to get the string equivalent of "First", in this case "dreadnaut" without quotes naturally.

How do I do that in PHP?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: JSON and PHP - Help!

Post by requinix »

Decode it, first. You'll get back an object with a "character_list" property which is an array containing one object. (You can get associative arrays back instead of objects, if you prefer working with those.) If you think there might be more than one, use a foreach loop, otherwise you can go directly to the one that's there with [0].

Code: Select all

->character_list[0]->name->first_lower
If you want to get to character-ms or total-ms, decode the JSON to an associative array: it's nicer than dealing with the object.

Code: Select all

["timing"]["character-ms"]
codex561
Forum Newbie
Posts: 4
Joined: Tue May 13, 2014 9:54 pm

Re: JSON and PHP - Help!

Post by codex561 »

requinix wrote:Decode it, first. You'll get back an object with a "character_list" property which is an array containing one object. (You can get associative arrays back instead of objects, if you prefer working with those.) If you think there might be more than one, use a foreach loop, otherwise you can go directly to the one that's there with [0].

Code: Select all

->character_list[0]->name->first_lower
If you want to get to character-ms or total-ms, decode the JSON to an associative array: it's nicer than dealing with the object.

Code: Select all

["timing"]["character-ms"]
But how do I echo it out?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: JSON and PHP - Help!

Post by requinix »

The same way you would echo out any value: just do it.

Code: Select all

echo $var->character_list[0]->name->first_lower;
codex561
Forum Newbie
Posts: 4
Joined: Tue May 13, 2014 9:54 pm

Re: JSON and PHP - Help!

Post by codex561 »

requinix wrote:The same way you would echo out any value: just do it.

Code: Select all

echo $var->character_list[0]->name->first_lower;
I feel really stupid right now, I have never worked with JSON before but I tried this and it did not work

Code: Select all

$var='{"character_list":[{"name":{"first":"Dreadnaut","first_lower":"dreadnaut"},"battle_rank":{"percent_to_next":"0","value":"100"}}],"returned":1,"timing":{"character-ms":4,"total-ms":4}}';

	echo $var->character_list[0]->name->first_lower;  
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: JSON and PHP - Help!

Post by requinix »

How about we keep this to just one thread, okay?

You have to decode it. Re-read my first post.
Post Reply