zwitterion zwitterion - 3 years ago 74
PHP Question

How to filter the follow JSON using PHP?

I am performing a query against a Payment Gateway and this is part of a JSON I am receiving as reply:

"//{\"COLUMNS\":[\"TR_ID\",\"INV_ID_EXT\",\"CUST_ID_EXT\",\"MERCH_ID\", ... ,null,\"510510\",1,24]]}"

How can I delete from the string the "\" to get transform in a real JSON?

What I did so far:

echo $JSON2 = str_replace("//","",$theString);

I get this:

"{\"COLUMNS\":[\"TR_ID\", ...

When I try
I still have the
"{\"COLUMNS\":[\"TR_ID\", ...

For some reason I am able to remove the
but not

How could I remove the
from this string without loop character by character?

Answer Source

Have you tried like this using str_replace and stripslashes? see here

$string="//{\"COLUMNS\":[\"TR_ID\",\"INV_ID_EXT\",\"CUST_ID_EXT\",\"MERCH_ID\", ... ,null,\"510510\",1,24]]}";

echo stripslashes(str_replace('//','',$string));

N.B: This will remove // and \ characters from your string but still it's not a valid json. A valid json will be like this, so in that case you've to also remove the last ] and ... from the string

    "COLUMNS": ["TR_ID", "INV_ID_EXT", "CUST_ID_EXT", "MERCH_ID", null, "510510", 1, 24]

For that :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download