I've now done a bit more on this. You have to lose the square brackets, they're invalid even in PHP (except perhaps if the expression is treated initially as a pure array in more recent versions of PHP which I believe might accept array literals, even then the contained JSON string would probably need single quotes around it). In javascript, they can serve no valid purpose.
That said, I've refined all three of my previous methods to use RegEx to recognize a single nested JSON string and if present decode/parse it as well into the containing JSON object without having to know in advance what it's name is. If there is no nested JSON, that will be decoded/parsed normally. If there are more than one nested JSON string, it will not work, but something could probably be worked out to accommodate that by counting the number of matches in the array that the RegEx produces. But that could be trickier than I'm thinking.
Here they are in the same order as before:
PHP Code:
<pre>
<?php
$json = '{"invId":230,"invNum":"1402001","invDate":"2014-03-18","invProject":"0420.020","invStatus":"DRAFT","invCycle":"E","invPosted":"N","invJson":"{\"InvInfo\":{\"Number\":\"1402001\",\"Date\":\"2014-03-18\",\"FmDate\":\"2014-02-01\",\"ToDate\":\"2014-02-28\",\"Type\":\"DRAFT\",\"Cycle\":\"E\",\"Posted\":\"N\",\"Memo\":\"\",\"Total\":6877.5,\"Outstanding\":10716.29,\"TotalPaid\":0,\"TotalCurrent\":0,\"Total30Days\":0,\"Total60Days\":0,\"Total90Days\":10716.29},\"ProjInfo\":{\"ID\":\"0420.020\",\"Name\":\"Old Republic Title: Concord\",\"ValueOrig\":\"0.00\",\"ValueAmend\":\"0.00\",\"ValueReimb\":\"0.00\",\"Mult\":\"0.15\",\"RateGroup\":\"8\",\"ReferTo\":\"Mr. Neil Wirth\",\"PrintPB\":\"N\",\"StartDate\":\"2013-11-07\"},\"CompInfo\":{\"Name\":\"Old Republic Title Company\",\"Address1\":\"1001 Galaxy Way, Suite 100\",\"Address2\":null,\"Address3\":null,\"City\":\"Concord\",\"State\":\"CA\",\"ZipCode\":\"94520\",\"Contact\":\"Accounts Payable\"},\"SqFtInfo\":[],\"HrlyInfo\":{\"1\":{\"Phase\":\"216\",\"Description\":\"Additional Services\",\"Date\":\"2014-02-03\",\"EmpNum\":\"C183\",\"StaffDesc\":\"Professional Staff I\",\"Hours\":\"65.50\",\"Rate\":105,\"Comment\":\"No comment\"},\"2\":{\"Phase\":\"216\",\"Comment\":\"BilledToDate\",\"Amount\":3330}},\"FxFeInfo\":[],\"ConsInfo\":[],\"ExpsInfo\":[],\"AdjsInfo\":{\"Services\":{\"Text\":\"(Amount Previously Billed - $0.00) Adjustment\",\"PrevBilled\":0,\"TypeTotal\":6877.5,\"Amount\":0,\"Total\":6877.5},\"Expenses\":{\"Text\":\"Adjustment\",\"PrevBilled\":0,\"TypeTotal\":0,\"Amount\":0,\"Total\":0}}}"}';
$jsonobj = json_decode($json, true);
$jsonmatch = preg_match('/, *"([^"]+)" *: *"\{/', $json, $jsonar);
if($jsonmatch){
$jsonobj[$jsonar[1]] = json_decode($jsonobj[$jsonar[1]], true);
}
print_r($jsonobj);
echo '<br>';
echo $jsonobj['invJson']['InvInfo']['FmDate'];
?>
</pre>
Code:
var rawjson = '{"invId":230,"invNum":"1402001","invDate":"2014-03-18","invProject":"0420.020","invStatus":"DRAFT","invCycle":"E","invPosted":"N","invJson":"{\"InvInfo\":{\"Number\":\"1402001\",\"Date\":\"2014-03-18\",\"FmDate\":\"2014-02-01\",\"ToDate\":\"2014-02-28\",\"Type\":\"DRAFT\",\"Cycle\":\"E\",\"Posted\":\"N\",\"Memo\":\"\",\"Total\":6877.5,\"Outstanding\":10716.29,\"TotalPaid\":0,\"TotalCurrent\":0,\"Total30Days\":0,\"Total60Days\":0,\"Total90Days\":10716.29},\"ProjInfo\":{\"ID\":\"0420.020\",\"Name\":\"Old Republic Title: Concord\",\"ValueOrig\":\"0.00\",\"ValueAmend\":\"0.00\",\"ValueReimb\":\"0.00\",\"Mult\":\"0.15\",\"RateGroup\":\"8\",\"ReferTo\":\"Mr. Neil Wirth\",\"PrintPB\":\"N\",\"StartDate\":\"2013-11-07\"},\"CompInfo\":{\"Name\":\"Old Republic Title Company\",\"Address1\":\"1001 Galaxy Way, Suite 100\",\"Address2\":null,\"Address3\":null,\"City\":\"Concord\",\"State\":\"CA\",\"ZipCode\":\"94520\",\"Contact\":\"Accounts Payable\"},\"SqFtInfo\":[],\"HrlyInfo\":{\"1\":{\"Phase\":\"216\",\"Description\":\"Additional Services\",\"Date\":\"2014-02-03\",\"EmpNum\":\"C183\",\"StaffDesc\":\"Professional Staff I\",\"Hours\":\"65.50\",\"Rate\":105,\"Comment\":\"No comment\"},\"2\":{\"Phase\":\"216\",\"Comment\":\"BilledToDate\",\"Amount\":3330}},\"FxFeInfo\":[],\"ConsInfo\":[],\"ExpsInfo\":[],\"AdjsInfo\":{\"Services\":{\"Text\":\"(Amount Previously Billed - $0.00) Adjustment\",\"PrevBilled\":0,\"TypeTotal\":6877.5,\"Amount\":0,\"Total\":6877.5},\"Expenses\":{\"Text\":\"Adjustment\",\"PrevBilled\":0,\"TypeTotal\":0,\"Amount\":0,\"Total\":0}}}"}';
var jsonm = /, *"([^"]+)" *: *"\{/.exec(rawjson);
if(jsonm){
var jsonar = rawjson.split(jsonm[0]);
var jsonobj = $.parseJSON(jsonar[0] + '}');
var raw2 = '{' + jsonar[1].substring(0, jsonar[1].lastIndexOf('"'));
var jsonobj2 = $.parseJSON(raw2);
jsonobj[jsonm[1]] = jsonobj2;
} else {
var jsonobj = $.parseJSON(rawjson);
}
document.write(jsonobj['invJson']['AdjsInfo']['Services']['Text']);
Note - this last (below) is with the double escapes in the nested JSON string, which, again would have to be added on the server side, which can be done with (in PHP) preg_replace('/\\\"/', '\\\\"', $json);
Code:
var rawjson = '{"invId":230,"invNum":"1402001","invDate":"2014-03-18","invProject":"0420.020","invStatus":"DRAFT","invCycle":"E","invPosted":"N","invJson":"{\\"InvInfo\\":{\\"Number\\":\\"1402001\\",\\"Date\\":\\"2014-03-18\\",\\"FmDate\\":\\"2014-02-01\\",\\"ToDate\\":\\"2014-02-28\\",\\"Type\\":\\"DRAFT\\",\\"Cycle\\":\\"E\\",\\"Posted\\":\\"N\\",\\"Memo\\":\\"\\",\\"Total\\":6877.5,\\"Outstanding\\":10716.29,\\"TotalPaid\\":0,\\"TotalCurrent\\":0,\\"Total30Days\\":0,\\"Total60Days\\":0,\\"Total90Days\\":10716.29},\\"ProjInfo\\":{\\"ID\\":\\"0420.020\\",\\"Name\\":\\"Old Republic Title: Concord\\",\\"ValueOrig\\":\\"0.00\\",\\"ValueAmend\\":\\"0.00\\",\\"ValueReimb\\":\\"0.00\\",\\"Mult\\":\\"0.15\\",\\"RateGroup\\":\\"8\\",\\"ReferTo\\":\\"Mr. Neil Wirth\\",\\"PrintPB\\":\\"N\\",\\"StartDate\\":\\"2013-11-07\\"},\\"CompInfo\\":{\\"Name\\":\\"Old Republic Title Company\\",\\"Address1\\":\\"1001 Galaxy Way, Suite 100\\",\\"Address2\\":null,\\"Address3\\":null,\\"City\\":\\"Concord\\",\\"State\\":\\"CA\\",\\"ZipCode\\":\\"94520\\",\\"Contact\\":\\"Accounts Payable\\"},\\"SqFtInfo\\":[],\\"HrlyInfo\\":{\\"1\\":{\\"Phase\\":\\"216\\",\\"Description\\":\\"Additional Services\\",\\"Date\\":\\"2014-02-03\\",\\"EmpNum\\":\\"C183\\",\\"StaffDesc\\":\\"Professional Staff I\\",\\"Hours\\":\\"65.50\\",\\"Rate\\":105,\\"Comment\\":\\"No comment\\"},\\"2\\":{\\"Phase\\":\\"216\\",\\"Comment\\":\\"BilledToDate\\",\\"Amount\\":3330}},\\"FxFeInfo\\":[],\\"ConsInfo\\":[],\\"ExpsInfo\\":[],\\"AdjsInfo\\":{\\"Services\\":{\\"Text\\":\\"(Amount Previously Billed - $0.00) Adjustment\\",\\"PrevBilled\\":0,\\"TypeTotal\\":6877.5,\\"Amount\\":0,\\"Total\\":6877.5},\\"Expenses\\":{\\"Text\\":\\"Adjustment\\",\\"PrevBilled\\":0,\\"TypeTotal\\":0,\\"Amount\\":0,\\"Total\\":0}}}"}';
var jsonobj = $.parseJSON(rawjson);
var jsonm = /, *"([^"]+)" *: *"\{/.exec(rawjson);
if(jsonm){
jsonobj[jsonm[1]] = $.parseJSON(jsonobj[jsonm[1]]);
}
document.write(jsonobj['invJson']['AdjsInfo']['Services']['Text']);
Bookmarks