Jump to content

Json


djp1988

Recommended Posts

I am trying to make a facebook app, and for the publisher section, I need to write a php script that outputs some JSON, part of which is the markup used by facebook to render my content, a very simplifyed example of this is here:

{"content":	{"fbml":"Word of the Day<br /><input type='text' value='' name='word' />",	 "publishEnabled":true	}, "method":"publisher_getInterface"}

My "fbml" (Facebook markup language) I wanted to be rendered is more complexe, and I have <style> and <script>, the problem comes from the javascript within my markup, I have an object in the form of:

var array = [];array.push({"name":"mike","age":25});...

Obviously, the object notation of my javascript object of my markup, is conflicting with the JSON structure.What can I do to correctly parse this ?I have tried wrapping the contents of my <script> with <![CDATA[........]]> with no luck

Link to comment
Share on other sites

Have you seen exactly where the problem is? If the fbml is only a string it shouldn't be interfering. Just remember to substitute all line breaks of the string for "\n" because Javascript can't parse multiline strings.

Link to comment
Share on other sites

It seems the problem might not be what I originally thought, consider the following string content for the "fbml" part in the JSON:

$fbml = "<script>var words=[];words.push({'word':'Hello, I don't give a damn'});</script>";$content = '"fbml":"'.$fbml.'","publishEnabled":true';$json = '{"content":{'.$content.'},"method":"'.$method.'"}';

I thought the single quote in Hello, I don't give a damn would break the script, seen as it is itself a string, inside the markup. Seem as I have already assigned the double quotes to the entire string of the variable $fbml, I proceeded to escape the single quote in the phrase: Hello, I don\'t give a damnBut it seems this is the problem. So for my JSON to pass, I can't escape the quote, but I know that if my markup is unescaped, then I will run into problems later on. Or will I ?

Link to comment
Share on other sites

It seems the problem might not be what I originally thought, consider the following string content for the "fbml" part in the JSON:
$fbml = "<script>var words=[];words.push({'word':'Hello, I don't give a damn'});</script>";$content = '"fbml":"'.$fbml.'","publishEnabled":true';$json = '{"content":{'.$content.'},"method":"'.$method.'"}';

I thought the single quote in Hello, I don't give a damn would break the script, seen as it is itself a string, inside the markup. Seem as I have already assigned the double quotes to the entire string of the variable $fbml, I proceeded to escape the single quote in the phrase: Hello, I don\'t give a damnBut it seems this is the problem. So for my JSON to pass, I can't escape the quote, but I know that if my markup is unescaped, then I will run into problems later on. Or will I ?

In that case, just putting a backslash by the quote won't work, you need to make sure the backslash and the quote get passed so that they can be parsed after, try this: I don\\\'t give
Link to comment
Share on other sites

Also, for JSON all properties need to use double quotes, not single quotes. This will not work:{'word':'Hello, I don\'t give a damn'}this will:{"word":"Hello, I don't give a damn"}$fbml = '<script>var words=[];words.push({"word":"Hello, I don\'t give a damn"});</script>';

Link to comment
Share on other sites

$fbml = '<script>var words=[];words.push({"word":"Hello, I don\'t give a damn"});</script>';
Ah but i didn't want that object to get parsed, I eventually used PHP 5.2's json_encode() to encode my fbml, and with a single quote escape it passed...
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...