How to convert a date from SQL server?

时间:2015-06-25 18:27:08

标签: javascript php json

I have a query that brings data back from the database. I have php code to return the data in an array of arrays to convert to a JSON.

while($imageData = sqlsrv_fetch_array($result))
        {
            $images[] = array('FilePath' => str_replace($imageData['FilePath'],"\\","\\\\"), 
                'FileName' => $imageData['FileName'], 
                'FileExtension' => $imageData['FileExtension'], 
                'WholeNumber' => $imageData['Whole Nbr'], 
                'Type' => $imageData['Type'], 
                'Size' => $imageData['Size'], 
                'Revision' => $imageData['Revision'], 
                'OtherNumber' => $imageData['Other Nbr'], 
                'SheetNumber' => $imageData['Sheet Nbr'], 
                'SheetsTotal' => $imageData['Of Sheets'], 
                'FrameNumber' => $imageData['Frame Nbr'], 
                'FramesTotal' => $imageData['Of Frames'], 
                'DocumentTitle' => $imageData['Doc Title'], 
                'Volume' => $imageData['Volume'], 
                'Note' => $imageData['Note'], 
                'Print' => $imageData['Prnt'], 
                'Obs' => $imageData['Obs'], 
                'AcquireDate' => $imageData['Acquire Date'], 
                'Source' => $imageData['Source'], 
                'BaseDoc' => $imageData['Base Doc'], 
                'AccDocNbr' => $imageData['Acc Doc Nbr'], 
                'CommonSubDirectory' => $imageData['CommonSubDirectory']);
        }   

Javascript then converts it to a JSON:

function SendRequest(source, params) {

    var http = new XMLHttpRequest();

    try {

        http.open("GET", source + '?' + params, false);
        http.setRequestHeader("Content-type","application/json");
        http.onload = function() {
            //alert('in function');
        }
        http.send(params);
    }

    catch (err) {
        alert(err.message);
    }

    return JSON.parse(http.responseText);

}

Then getting data back like this allows me to access all the members EXCEPT the AcquiredDate

data = SendRequest;
var newText  = document.createTextNode(data[r].Type);

But this does not work var newText = document.createTextNode(data[r].AcquireDate);

I get an object back. I can print it out with

alert(JSON.stringify(data[r].AcquireDate));

Which looks like this: {"date":"2010-04-28 00:00:00","timezone_type":3,"timezone":"Europe/Paris"}

But if I try to parse it I get an error

acquireDate = JSON.parse(data[r].AcquireDate).date;

or acquireDateObj = JSON.parse(data[r].AcquireDate); acquireDate = acquireDateObj.date;

I get JSON.parse: unexpected character at line 1 column 2 of the JSON data

Why can JSON stringify but not parse? And what is column 2? A quote? The caharcter d? Both of those look valid to me.

1 个答案:

答案 0 :(得分:0)

I figured it out. I don't know why I need to stringify first, but this works. acquireDate = JSON.parse(JSON.stringify(imageData[r].AcquireDate)).date; Even though the original parent object looks the same as the child object and JSON parse works with the parent.