재우니의 블로그

 

json 으로 넘어온 string 값을 parseJSON 으로 json 객체를 만들고, 이를 for 문으로 추출해 오는 방법

 

/********************************** example **************************************/
 
var base_url = 'http://people.cs.uct.ac.za/~swatermeyer/VulaMobi/';
 
function example()
{
    var response = "";
    var form_data = {
        username: username,
        password: password
    };
    $.ajax({
        type: "POST",
        url: base_url + "ajax.php?test/json",
        data: form_data,
        success: function(response)
        {
            /*response = '[{"Language":"jQuery","ID":"1"},{"Language":"C#","ID":"2"},
{"Language":"PHP","ID":"3"},{"Language":"Java","ID":"4"},
                           {"Language":"Python","ID":"5"},{"Language":"Perl","ID":"6"},
                           {"Language":"C++","ID":"7"},{"Language":"ASP","ID":"8"},
                           {"Language":"Ruby","ID":"9"}]'*/
            console.log(response);
           
    var json_obj = $.parseJSON(response);//parse JSON
           
            var output="<ul>";
            for (var i in json_obj)
            {
                output+="<li>" + json_obj[i].Language + "" + json_obj[i].ID + "</li>";
            }
            output+="</ul>";
           
            $('span').html(output);
        },
        dataType: "json"//set to JSON   
    })   
}