...

View Full Version : Is this valid JSON?



Zegg90
01-15-2008, 04:19 PM
I am new to JSON, so I am just trying stuff out for my PHP application before I use it.

I have a question, is this valid JSON?


{"name": "Test Package", /* Required or 'Untitled' (default value) */
"author": "Zeggy", /* Optional, maybe a default value */
"version": 1.1, /* Version, must be a number */
"actions": [
{
"name": "Adding test code", /* Name for this particular process, required */
"file": "edit_me.php", /* Required */
"action": "add", /* choose out of [add, remove, replace], is required */
"type": "before", /* before, after */
"where": "$test = \"123321\";", /* Code to search for */
"code": "blabla
blabla
more code" /*Code to add/remove/replace */
},
{
"name": "Removing a comment",
"file": "another_file.php", /* Required */
"action": "remove",
"code": "\/\/This is a PHP comment!\\n" /* Removing this comment, 'where' not required */
},
{
"name": "Replacing $var1",
"file": "replace.php", /* Required */
"action": "replace",
"where": "$var1 = \"var numba 1\";", /* Find this string */
"code": "$var2 = \"new var #2\";" /* Replace with this */
}
]
}


Would this work if I used PHP to change it into an object/array?
And are the comments allowed?
Any other observations? :)

Thanks!

shyam
01-15-2008, 06:17 PM
{"package": {
"name": "Test Package", /* Required or 'Untitled' (default value) */
"author": "Zeggy", /* Optional, maybe a default value */
"version": 1.1, /* Version, must be a number */
"actions": [
{
"name": "Adding test code", /* Name for this particular process, required */
"file": "edit_me.php", /* Required */
"action": "add", /* choose out of [add, remove, replace], is required */
"type": "before", /* before, after */
"where": "$test = \"123321\";", /* Code to search for */
"code": "blabla
blabla
more code" /*Code to add/remove/replace */
},
{
"name": "Removing a comment",
"file": "another_file.php", /* Required */
"action": "remove",
"code": "\/\/This is a PHP comment!\\n" /* Removing this comment, 'where' not required */
},
{
"name": "Replacing $var1",
"file": "replace.php", /* Required */
"action": "replace",
"where": "$var1 = \"var numba 1\";", /* Find this string */
"code": "$var2 = \"new var #2\";" /* Replace with this */
}
]
}}

its valid json...comments are allowed...except for the portion highlighted in red...strings in javascript have to be terminated in the same line...but both IE and FF allow the C style continutation using the \

so, u can either write

"code": "blabla blabla more code"
or

"code": "blabla\
blabla\
more code"

Zegg90
01-15-2008, 07:07 PM
Okay, thanks :)
I'm using PHP to interpret the JSON objects, so multi-lines seem to work (with \n).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum