0

I have this code converting a mysql query to json:

$sth = mysql_query('SELECT * FROM `staff` ORDER BY `id` DESC LIMIT 20') or die(mysql_error());
$rows = array();
while($r = mysql_fetch_array($sth)) {
    $rows[] = $r;
}
print json_encode($rows);

Works great, but i have in my database, a url field, here is a sample json row:

{"0":"4","id":"4","1":"Noter 2","name":"Noter 2","2":null,"desc":null,"3":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","iconurl":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","4":"1262032317","date":"1262032317","5":"dBelement, LLC","company":"dBelement, LLC","6":"http:\/\/dbelement.com\/","companyurl":"http:\/\/dbelement.com\/","7":"http:\/\/noter2.dbelement.com","appurl":"http:\/\/noter2.dbelement.com"},

How can i get around this?

2
  • look at the url's passed in the string:..... they are backslash protected.... Commented Dec 28, 2009 at 22:36
  • @tarnfeld: That's not a problem. Commented Dec 28, 2009 at 22:38

2 Answers 2

1

I suppose the "problem" is the presence of backslashed in the URLs ?

Like :

http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg

Well, that's not a problem for Javascript : try to assign your JSON data to a Javascript object, and display one of those URL :

var data = {"0":"4","id":"4","1":"Noter 2","name":"Noter 2","2":null,"desc":null,"3":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","iconurl":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","4":"1262032317","date":"1262032317","5":"dBelement, LLC","company":"dBelement, LLC","6":"http:\/\/dbelement.com\/","companyurl":"http:\/\/dbelement.com\/","7":"http:\/\/noter2.dbelement.com","appurl":"http:\/\/noter2.dbelement.com"};
alert(data[3]);

The URL is displayed correctly :

http://images.apple.com/webapps/productivity/images/noter2_20091223182720-thumb.jpg

So, the backslashes are not a "problem" ;-)


And, in fact, if you take a look at the "string" section of json.org, you'll see that slashes have to be backslashed for your string to be valid.

Sign up to request clarification or add additional context in comments.

1 Comment

it already is, perhaps it is nothing to do with this :/ ok thanks.
0

I don't see a problem here. If you're wondering why the URLs look like this:

http:\/\/www.example.com\/

Instead of like this:

http://www.example.com/

It's because the / characters are being appropriately escaped. When you unserialize the JSON object in javascript, the URLs will look as they do in your database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.