-1

I have number of records in a table (oracle). i am using

 $json_OutputArray = array();


    while ($result = oci_fetch_assoc($parse_submitted_list)) {
        $json_OutputArray[] = $result;
    }

    $jsonstring = (json_encode($json_OutputArray));

    echo '{"value":"' . $jsonstring . '"}';

$jsonstring holds keyvalue pair like

{"value":"[{"APPLICATION_NUMBER":"1","FINAL_SUBMISSION_DONE_ONE":"11-SEP-14","EBY_ECODE":"3621","EBY_ECODE_S":"4","SEND_FOR_REVIEW_LEVEL_1_ON":"12-SEP-14","APPLICATION_STATUS_CODE":"1","RESUBMITTED_ON":"12-SEP-14","SEND_FOR_REVIEW_LEVEL_2_ON":null}]"}

I am using ajax to get this value with post method and json datatype

$.ajax({
            url: '../PhpProject1/GetValues.php',
            dataType: 'json',
            type: 'POST',
            data: {"fromDate": fromDate, "toDate": toDate},
            success: function(response) {
                var jsonstring = response.value;        
                alert(jsonstring);
                       var obj = JSON.parse(jsonstring);
                alert(obj.APPLICATION_NUMBER);  



            },
            error: function(x, e) {
                if (x.status === 0) {
                    alert('You are offline!!\n Please Check Your Network.');
                } else if (x.status === 404) {
                    alert('Requested URL not found.');
                } else if (x.status === 500) {
                    alert('Internel Server Error - Please try after relogin to the application');
                } else if (e === 'parsererror') {
                    alert('Parsing JSON Request failed.');
                } else if (e === 'timeout') {
                    alert('Request Time out.');
                } else {
                    alert('Unknow Error.\n' + x.responseText);
                }
            }
        });

How to decode this json string in javascript by each record.thanks in advance.

Shahanas
  • 25
  • 1
  • 1
  • 8
  • 1
    your json is invalid: http://jsonlint.com/ – Kamlesh Arya Sep 25 '14 at 05:36
  • 1
    You are producing invalid JSON. Why are not simply doing: `echo json_encode(array('value' => $json_OutputArray));`? You already seem to know how to generate JSON with `json_encode`? Why are you trying to generate it "manually" at all? As for your actual question: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) – Felix Kling Sep 25 '14 at 05:37

3 Answers3

1

You can leave the task of creating the valid json string completely to json_encode.

instead of

$jsonstring = (json_encode($json_OutputArray));
echo '{"value":"' . $jsonstring . '"}';

do

echo json_encode( array( "value" => $json_OutputArray ) );

updated to comments below

In your Ajax call, you have your success function like

 success: function(response) {
      var jsonstring = response.value;        
      alert(jsonstring);
      var obj = JSON.parse(jsonstring);
      alert(obj.APPLICATION_NUMBER);  
 }

Here you dont need JSON.parse as the repsonse will be already a json object. note that you are mentioning the datatype as json. So loop over the result you just need to do.

 success: function(response) {
      var array = response.value;        
      for(var i=0;i<array.length;i++) {
          alert(array[i].APPLICATION_NUMBER);  
      }          
 } 
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • @Shahanas : post the jquery code you are having now in the question. there are multiple ways to make an ajax request in jquery. – Mithun Satheesh Sep 25 '14 at 05:41
0

You can use $.get() to fetch the JSON then you can use it in your page.

An example would look like this:

 $.get(url, function(json){
    //your json data is now as the json variable
 });

Then you can parse through it as follows:

 $.get(url, function(json){
    //your json data is now as the json variable
    var value = json.value;
 });
Control Freak
  • 12,965
  • 30
  • 94
  • 145
0

First of all, your JSON string is invalid and any parser would not be able to decode it. The valid form of your JSON string is [using this to validate your strings]:

{
    "value": [
        {
            "APPLICATION_NUMBER": "1",
            "FINAL_SUBMISSION_DONE_ONE": "11-SEP-14",
            "EBY_ECODE": "3621",
            "EBY_ECODE_S": "4",
            "SEND_FOR_REVIEW_LEVEL_1_ON": "12-SEP-14",
            "APPLICATION_STATUS_CODE": "1",
            "RESUBMITTED_ON": "12-SEP-14",
            "SEND_FOR_REVIEW_LEVEL_2_ON": null
        }
    ]
}

Then simply use $.get() to query the json string and the parseJSON() function to parse your string

$.get(url,function(jsonString))
{
    var data = $.parseJSON(jsonString);
    var app_no = data.value[0].APPLICATION_NUMBER;
}
johngreen
  • 2,676
  • 5
  • 32
  • 47