Results 1 to 8 of 8

Thread: How to refer to JSON array

  1. #1
    Join Date
    Feb 2013
    Location
    California
    Posts
    86
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How to refer to JSON array

    I have the following JSON returned from a database query.

    Code:
    [{"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}}}"}]
    You will note that within the JSON is a JSON (invJson). My question is how do I obtain the value of a field within the embedded JSON area? For example how would I get the value of 'Name' within the 'ProjInfo' of the 'invJson'?

    To get the 'invNum' I use
    Code:
    var oInvoice = $.parseJSON(data);
    var gInv_Num = oInvoice[0].invNum;
    I have tried everything that I can think of to no avail.

    TIA for any assistance.
    jdadwilson

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    That's a JSON string encoded as a JSON string (i.e., it's double-encoded). You'll need to parse it again.

    Code:
    var oInvoice = $.parseJSON(data);
    var gInv_Num = $.parseJSON( oInvoice[0].invNum );
    Just as a side note, it would be good advice to not store JSON in a database. You should store the actual data in a database, and then json-encode it when needed. You'd probably be able to avoid double-encoding/decoding things that way, too.
    Last edited by jscheuer1; 04-08-2014 at 05:47 PM. Reason: typo

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    $.parseJSON() is for a string representing a strict JSON object. What you have in your post ("I have the following JSON") is a literal array.

    If that's already an array, you don't need $.parseJSON(). If it's a string, it's malformed JSON and must be processed some other way. If it's something else, well we shall see. We need to find out what you have before trying to process it:

    Code:
    alert(typeof data);
    alert(data.constructor);
    var oInvoice = $.parseJSON(data);
    var gInv_Num = oInvoice[0].invNum;
    Let me know what those two alerts give you. And there should be a way to proceed from there.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by jscheuer1 View Post
    $.parseJSON() is for a string representing a strict JSON object. What you have in your post ("I have the following JSON") is a literal array.

    If that's already an array, you don't need $.parseJSON(). If it's a string, it's malformed JSON and must be processed some other way…
    hmm… I had assumed that it actually was a string (e.g., a raw HTTP response) and I added quotes around it when I tested. However, I did my testing with PHP's native functions—I see now that it does not parse correctly in JS. However, JSONlint says it's valid…?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, it might have to be parsed server side then. A relatively simple PHP script could be called via AJAX or jQuery.ajax if need be.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, I've been playing around with this. Note I've replaced [ and ] with ' and ' for the raw string in all three below cases. In pure PHP, you can do:

    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($jsontrue);
    $jsonobj['invJson'] = json_decode($jsonobj['invJson'], true);
    print_r($jsonobj);
    echo 
    '<br>';
    echo 
    $jsonobj['invId'];
    ?>
    </pre>
    In jQuery you can do (string as is except for replacing the square brackets with single quote marks):

    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 jsonspliton = ',"invJson"';
    var jsonar = rawjson.split(jsonspliton);
    var jsonobj = $.parseJSON(jsonar[0] + '}');
    var raw2 = jsonar[1].substring(jsonar[1].indexOf('{'), jsonar[1].lastIndexOf('"'));
    var jsonobj2 = $.parseJSON(raw2);
    jsonobj['invJson'] = jsonobj2;
    document.write(jsonobj['invJson']['InvInfo']['FmDate']);
    Or (also in jQuery, string double escaped before processing):

    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);
    jsonobj['invJson'] = $.parseJSON(jsonobj['invJson']);
    document.write(jsonobj['invJson']['InvInfo']['FmDate']);
    To use the PHP method in your current code you would have to go to the PHP code each time via an AJAX call for each property you might want.

    Splitting the string as in the first jQuery method should be able to be performed on the data returned in your function more or less as is. Using the second jQuery method would require (I think) double escaping on the server side before passing it to jQuery. I don't think you could double escape it in javascript/jQuery.

    In both jQuery cases [] need to be removed if they are in fact in the string. Single quotes need not be added if the json is passed as a string.
    Last edited by jscheuer1; 04-09-2014 at 01:15 PM. Reason: had left in an unneeded diagnostic line in last code block
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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($jsontrue);
    $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']);
    Last edited by jscheuer1; 04-10-2014 at 06:18 AM. Reason: addslashes
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's a more thorough demo of the third method:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    
    </head>
    <body>
    <div id="result"></div>
    <script type="text/javascript">
    $.ajax({
    	url: 'json4.php',
    	cache: false,
    	success: function(data){
    		var jsonobj = $.parseJSON(data);
    		var jsonm = /, *"([^"]+)" *: *"\{/.exec(data);
    		if(jsonm){
    			jsonobj[jsonm[1]] = $.parseJSON(jsonobj[jsonm[1]]);
    		}
    		$('#result').html(jsonobj['invJson']['AdjsInfo']['Services']['Text']);
    	},
    	error: function(o, s, e){
    		window.console && console.log(s);
    	}
    });
    </script>
    </body>
    </html>
    json4.php:

    PHP Code:
    <?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}}}"}';
    echo 
    preg_replace('/\\\"/''\\\\"'$json);
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Resolved array jSon
    By ggalan in forum PHP
    Replies: 2
    Last Post: 07-26-2011, 02:30 AM
  2. Resolved Getting two values from AJAX json array
    By auriaks in forum JavaScript
    Replies: 12
    Last Post: 04-13-2011, 10:38 PM
  3. help with refer based redirect
    By chrisl12341234 in forum PHP
    Replies: 2
    Last Post: 12-10-2008, 07:14 AM
  4. how do I refer two forms to the same function?
    By golmiami in forum JavaScript
    Replies: 1
    Last Post: 06-02-2008, 03:19 AM
  5. htaccess allow & deny refer
    By taydu in forum Other
    Replies: 1
    Last Post: 12-04-2007, 06:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •