json_encode() Driving me Crazy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

json_encode() Driving me Crazy

Post by Pavilion »

Hello:

I've got an application where php constructs a dynamic table. The table is working great, even though it's constructed in the php processing file and passed via a $.getJSON() call. I've got a jQuery dataTable working with it, etc...

Now for the problem. Every row to my table has a <input type="checkbox. This checkbox is used for selecting records. 90% of the time the records will be selected for editing or deletion, at which point I just grabe the id='" . $activityID . "' and run from there.

But.. . there are occassions where I need to grab an array of data. So... my php constructs that particular control as follows:

Code: Select all

"<input type='checkbox' class='editck' id='editCk_" . $activityID . "' value=" . json_encode($rowArray) . "></input>"
When I echo the json_encode($rowArray) in a separate <td></td> - it echoes out the following:
{"startTM":"2013-08-07 11:15:00","endTM":"2013-08-29 13:45:00","Owner":"Duck, Donald","ActivityID":"369"}
After the entire table is constructed, I then echo the whole table as follows:

Code: Select all

echo(json_encode($outPut)); // $outPut is the variable containing the entire table object
So... here's the problem I'm running into:

When I grab the table with jQuery, I can't run JSON.parse() on it without error. Specifically the error is JSON.parse: unterminated Strng

To make a very long story short, in all my testing the parsing breaks at {"startTM":"2013-08-07.

When I copy and paste the actual array values into jQuery variable and then parse the variable, everything works like a charm. Something is happening with the JSON. I'm wondering a few things:
  1. Does it muck up things to json_encode specific table row values, and then json_encode the whole table for transport back to the client side?
  2. If json_encoding data twice before sending it back to the client side is a no-no, then how do I handle a situation like this, I tried NOT json_encoding the $rowArray, but that just created a different set of problems.
My array is constructed as follows:

Code: Select all

            $rowArray = array(
			'startTM'=>stripslashes($row['StartTime']),
			'endTM'=>stripslashes($row['EndTime']),
			'Owner'=>stripslashes($row['Owner']),
			'ActivityID'=>stripslashes($activityID),
            );
Any assistance with this problem would be greatly appreciated. It is a real stumper. The table is working great, my users love it. There is one task I need to complete, which uses data from this array. After I complete that task, I can move onto another portion of this project, and close up development on this table.

Thanks in advance - Pavilion
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: json_encode() Driving me Crazy

Post by Pavilion »

It's the spaces that are causing problems. When I strip all the spaces from my array, the JSON parsing works great. Following is the way my array is assembled right now.

Code: Select all

            $rowArray = array(
			'startTM'=>str_replace(' ', '', stripslashes(trim($row['StartTime']))),
			'endTM'=>str_replace(' ', '', stripslashes(trim($row['EndTime']))),
			'Owner'=>str_replace(' ', '', stripslashes(trim($row['Owner']))),
			'ActivityID'=>stripslashes($activityID)
            );
Then when I grab the row value in jQuery and parse it - things run smoothly:

[syntax] rowContent_Arry = $.parseJSON($(this).val());
console.log("dump content array: " + JSON.stringify(rowContent_Arry));[/syntax]

It is important that I find a successful way to pass arrays to jQuery WITH spaces in them. Does anyone have any suggestions?
  1. The application I'm working on will need to store arrays as table row values
  2. I'll need to pickup those stored arrays, parse them and use data
  3. Some of those arrays will have subject titles (that will include spacing), to say nothing of the date/time values with spaces between the date and the time, or owner values with a space between LastName and FirstName.
There has to be a successful way to handle the spacing and any suggestions are welcome.
Thanks in advance - Pavilion
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: json_encode() Driving me Crazy

Post by Christopher »

I just did:

Code: Select all

JSON.parse('{"startTM":"2013-08-07 11:15:00","endTM":"2013-08-29 13:45:00","Owner":"Duck, Donald","ActivityID":"369"}')
and it worked fine. The result is an object with the properties and values given. I suspect something else is going on. Are then other whitespace characters in the string? What version of jQuery are you using?
(#10850)
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: json_encode() Driving me Crazy

Post by Pavilion »

Christopher wrote:I just did:

Code: Select all

JSON.parse('{"startTM":"2013-08-07 11:15:00","endTM":"2013-08-29 13:45:00","Owner":"Duck, Donald","ActivityID":"369"}')
and it worked fine. The result is an object with the properties and values given. I suspect something else is going on. Are then other whitespace characters in the string? What version of jQuery are you using?
Your suspicions were correct, there was something else going on. As went over the script one more time I found I forgot to include single quotes in my input script. It was:

Code: Select all

<input type='checkbox' class='editck' id='editCk_" . $activityID . "' value=" . json_encode($rowArray) . "></input>
Rather than

Code: Select all

<input type='checkbox' class='editck' id='editCk_" . $activityID . "' value='" . json_encode($rowArray) . "'></input>
It doesn't look all that different at first glance, but look closely and value= has a single quote BEFORE the double quote. That's where I messed up.

Thanks for your patience and help - Pavilion
Post Reply