1

the last.fm API is returning a JSON string which I convert to an array using

json_decode($response, ($returnType == 'array'));

When the response is dumped it gives me the following json string:

{"session":{"name":"turncoat9","key":"b311abDee2d34e07541eeeefted2bad","subscriber":"0"}} 1

(note the 1 in the end)

Because of this extra character the json string cannot be converted to an associative array, hence my problem.

I found a few discussions which state that the 1 is a BOM (byte order mask) character, but all solutions provided does not work with my json string.

What is this character? Why is it there and why does this not happen when I develop locally? What is the solution for this?

Tried this on two different webservers, gave the same result

kendepelchin
  • 2,680
  • 2
  • 21
  • 25

2 Answers2

0

You can remove it using trim:

$cleaned_json = trim($received_json, '1 ');

Manual

adrien
  • 4,399
  • 26
  • 26
  • try trim 1 as well as trim space if there is any - if you only trim 1 but not the trailing space - it is not actually a json format but a string – user769889 May 14 '12 at 14:47
0

First, I do not understand ($returnType == 'array'), you should only put TRUE there

assoc->When TRUE, returned objects will be converted into associative arrays.

Second, what answer do you get from your API? Are you sure it returns only the json string?

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • $returnType is an argument. So when this argument equals 'array' it will be true, so yes. There will stand 'true' :) Next: Everything works locally, so I wrote all my code based on the Json string and all of it is returned how I want it to be returned – kendepelchin May 14 '12 at 14:31
  • I am pretty sure you have an echo some place, maybe different file encoding? Closed `?>` and left some spaces after? – Mihai Iorga May 14 '12 at 15:05
  • But why would the echo be inside the json string? it's the response of last.fm where it is coming from I guess. – kendepelchin May 14 '12 at 15:22
  • @mihal lorga how can i check the file encoding? – kendepelchin May 14 '12 at 15:34
  • some editors save files *by mistake* as Unicode, open with your favorite editor and save the file as ANSI or UTF-8. – Mihai Iorga May 15 '12 at 06:11