0

I have an array returned by AJAX in jQuery. I wanted to iterate through the AJAX success result and take individual values from the JSON array. The data is returned in the form of a string. I'm using jQuery's $.ajax to get it from the server, which works fine.

//my View
$.ajax({
    type: "POST",
    url:"http://localhost:8888/CodeIgniter/index.php/user/usercontroller/search",//controller function
    cache: false,
    data:{"responsible1":res1},
    success: function(data21) { 
        alert(data21);
    });
});
//data 21 alerts the following
{"taskname":"Coding","projname":"Easy Wedding"} 
{"taskname":"Maintain","projname":"Easy Wedding"}
{"taskname":"Flow Chart","projname":"Fnn"}
{"taskname":"development in ","projname":"Fnn"}
{"taskname":"flow chart","projname":"Art gallery"}

How can I access only individual taskname and projectname or how can I convert data21 to an array

1
  • 2
    That's not a valid JSON object array. Are you sure that's the data that's being returned? It should look like [{...}, {...}, {...}]. Commented Nov 23, 2011 at 8:35

5 Answers 5

5

just add dataType: "json" to your request, and you will get your data as object but this would just work if you put the return data into these brackets: []

otherwise use this

data21 = $.parseJSON('[' + data21.replace(/\"}/g, '"},').replace(/,$/, "") + ']');
console.log(data21);

example

Sign up to request clarification or add additional context in comments.

1 Comment

yaa i have added datatype bt its nt wrking and its giving same alerts
1
success: function(data) { 
data = jQuery.parseJSON(data);  
//now you can access the individual keys like alert(data.taskname+ " : "+data.projname)
    alert(data.taskname+ " : "+data.projname);
});

1 Comment

yaa i can access if it alerts only ...{"taskname":"Coding","projname":"Easy Wedding"}......bt its not working with multiples ...{"taskname":"Coding","projname":"Easy Wedding"} {"taskname":"Maintain","projname":"Easy Wedding"} {"taskname":"Flow Chart","projname":"Fnn"}
0

As long as the JSON is valid, you should be able to access the object array with indexes.

If this is the data being returned:

[
    {"taskname":"Coding","projname":"Easy Wedding"},
    {"taskname":"Maintain","projname":"Easy Wedding"},
    {"taskname":"Flow Chart","projname":"Fnn"},
    {"taskname":"development in ","projname":"Fnn"},
    {"taskname":"flow chart","projname":"Art gallery"}
]

Then this will work:

$.ajax({
    type: "POST",
    url: "http://localhost:8888/CodeIgniter/index.php/user/usercontroller/search",
    cache: false,
    dataType: "json",
    data: { "responsible1":res1 },
    success: function(data) { 
        alert(data[0].taskname);
    });
});

Comments

0

May be this help you.

var  t = { 
   'rows': [
        {"taskname":"Coding","projname":"Easy Wedding"},
        {"taskname":"Maintain","projname":"Easy Wedding"},
        {"taskname":"Flow Chart","projname":"Fnn"},
        {"taskname":"development in ","projname":"Fnn"},
        {"taskname":"flow chart","projname":"Art gallery"}
    ]
}



$.each(t.rows,function(index,value){
    for(var i in value){
        alert(i+' -> '+value[i]);
    }
});

This is the way to read json data that i think. Where t is containing json data

EDIT :

I'm not familiar with codeigniter but if you put below code in plain php file and run it you will find the result you are looking for.

<?php
    $arr = array();
    $arr[] = array('taskname'=>'Coding','projname'=>'Easy Wedding');
    $arr[] = array('taskname'=>'Maintain','projname'=>'Easy Wedding');
    $arr[] = array('taskname'=>'Flow Chart','projname'=>'Fnn');
    $arr[] = array('taskname'=>'development in','projname'=>'Art gallery');
    echo json_encode(array('rows'=>$arr));
?>

It will create same json data as i created in javascript t variable.

3 Comments

how should i return json in correct format may be i am making mistake there so things are not working.. i want json to be returned as [ {"taskname":"Coding","projname":"Easy Wedding"}, {"taskname":"Maintain","projname":"Easy Wedding"}, {"taskname":"Flow Chart","projname":"Fnn"}, {"taskname":"development in ","projname":"Fnn"}, {"taskname":"flow chart","projname":"Art gallery"} ]
@ianace it's json_encode(). for further information you can check php.net/manual/en/function.json-encode.php
@ianace i'm not encoding json. I'm encoding php array name $arr. json_encode() will create javascript equivalent object( in this case json). that you can get in your ajax success call and iterate it to get your result.
0

Here is some code that may help:

From JSON to CI

1) CODEIGNITER

public function selectDetalleSolicitudByIdCliente() {
    $this->cliente_model->id_cliente = $_POST['id_cliente'];
    $data["cliente_detalle"] = $this->cliente_model->selectDetalleSolicitudByIdCliente();
    echo json_encode($data);
}

2) JQUERY

$.post(
    "cliente/selectDetalleSolicitudByIdCliente",
    {
        id_cliente: id_cliente
    },
    function(respuesta){
        $("#reporte2").html(">>" + respuesta);

        var respuesta = jQuery.parseJSON(respuesta);//parse json

        $.each(respuesta.cliente_detalle,function(index,value){
            for(var i in value){
                alert(i+' -> '+value[i]);
            }
     });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.