Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-30-2012, 03:09 PM   #1
Registered: May 2008
Distribution: OpenSuSE
Posts: 65

Rep: Reputation: 15
JSON issues

I'm having some trouble getting a JSON request to work properly. I'm trying to send some data from PHP to JS via a JSON object, but I'm not having any luck. The PHP code that does the transfer is as follows:

$output = array();

$string = $forecast->date->weekday . "<br /> <img src=" . $forecast->icon_url .
" /> <br /> " . $forecast->high->fahrenheit . "&nbsp;&nbsp;" . $forecast->low->fahrenheit . "<br />" . $forecast->conditions . "<br />" ;

$output["day1"] = $string;

echo json_encode($output);
All the variables listed are text/number values pulled from a JSON object (I have tested, and the code was read IN properly, so that's not the issue). I know this may not be the best way to do this, but for now I'm just trying to figure out how to get the transfer to work properly. The JavaScript that is interpreting it is below.

req.setRequestHeader("Content-Type", "text/plain; charset=UTF-8");
var data = $.parseJSON( "(" + req.responseText + ")" );
This code crashes when it gets to the parseJSON function, probably because it's receiving some mangled nastiness as input. When I print out req.responseText, I get the following:
{"day1":"Wednesday<br \=""> <img src="http:%5C/%5C/" \=""> <br \=""> 88&nbsp;&nbsp;57<br \="">Thunderstorm<br \="">","day2":""}
NOTE: the variables used above are, in order, "Wednesday", "", "88", "57", and "Thunderstorm"

Not sure what I'm doing wrong here, but any help would be appreciated. Thanks.

EDIT: I decided this was a pointless way to go about this, so instead I am sending raw JSON to be formatted client-side. However, the interpretation is still failing at the same line as before. I receive properly formatted data this time, as shown below, but the JSON interpreter fails every time. Any suggestions?
Addition info: I am getting "SyntaxError: JSON.parse: unexpected character" now that I wised up and used some error checking (kinda stupid not to have tried it before, but oh well). However, according to my JSON is perfectly valid.

Last edited by Tyler_H72; 05-30-2012 at 03:42 PM.
Old 05-31-2012, 03:26 AM   #2
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Rep: Reputation: 32
The piece of json is not valid because you have /= in it. I suspect that in the second case your not getting what you think your getting so put some debug lines in to check. I might suggest using something like jquery. to do what you want. I have had great success with it as it allows you to force the content type to be json thereby when you get the json object in your return function parsed and ready to be used for you
1 members found this post helpful.
Old 06-01-2012, 06:49 AM   #3
Registered: May 2008
Distribution: OpenSuSE
Posts: 65

Original Poster
Rep: Reputation: 15
My bad- I already solved this one but I forgot to post about it. You're right- I wasn't getting what I expected. when I printed the data out, I got the JSON listed above, but when I looked at it with FireBug, I saw that I was getting that data wrapped inside HTML and body tags- apparently I had put them in the PHP script for some reason. Works fine now though. Thanks for the help!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] echo javascript json array secretlydead Programming 2 02-11-2012 06:20 AM
What is Json and jQuery and how are they related to Javascript? Romanus81 Programming 1 08-13-2009 09:35 PM
LXer: Exploring JSON and CouchDB LXer Syndicated Linux News 0 04-07-2009 04:50 AM
json help sajith Programming 1 02-27-2008 06:38 AM
php-json fedora 5 hammer65 Fedora 0 07-15-2006 02:41 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration