3

私は次の文字列を持っています

{
    "role_name": "bhavin",
    "user_id": "50667afb9de0f2600b000001",
    "Par_user": "user",
    "Child_user_index": "user_index",
    "Child_user_profile": "user_profile",
    "Par_App": "App",
    "Child_App_new": "App_new",
    "Child_App_index": "App_index",
    "Par_App1": "App1",
    "Child_App1_index": "App1_index",
    "Par_App2": "App2",
    "Child_App2_index": "App2_index",
    "Child_App2_new": "App2_new",
    "controller": "users",
    "action": "save_permissions"
}​

以下を通過したい

{
    "role_name": "bhavin",
    "user_id": "50667afb9de0f2600b000001",
    "user": ["user_index", "user_profile"],
    "App": ["App_new", "App_index"],
    "App1": ["App1_index"],
    "App2": ["App2_index", "App2_new"],
    "controller": "users",
    "action": "save_permissions"
}​

コード:

$("#perm_save_button").click(function() { //alert("hiii");
    if ($("#role_name").val() == "") {
        alert("role name can't be blank");
        return false;
    }

    if ($('input:checkbox').is(":checked")) {

    } else {
        alert("Please Select any Privilages");
        return false;
    }
    var role_name = $('#role_name').val();
    var user_id = $("#app_rateid").val();
    if (($("#AllPrev").is(":checked"))) {

        var selected_value = new Object();

        $('input[checked=checked]').each(function() {
            $('span[class=checked]')
            var select = ($(this).attr('id'));
            var select_value = ($(this).attr('name'));
            selected_value[select] = select_value;
        });
        selected_value["role_name"] = role_name;
        selected_value["user_id"] = app_rateid;

        $.ajax({
            url: '<%= url_for :action => "save_permissions" %>',
            data: selected_value,
            dataType: 'json',
            success: function(data) {
                alert("101 Data Successfully Updated");
                $("#rma_options").dialog('close');
            }
        }); //ajax function end
    }
    else {
        var selected_value = new Object();
        selected_value["role_name"] = role_name;
        selected_value["user_id"] = user_id;
        $('input[checked=checked]').each(function() {
            var select = ($(this).attr('id'));
            var select_value = ($(this).attr('name'));
            select_value = select_value.split(/_(.+)?/)[1]
            alert(select_value);
            selected_value[select] = select_value;
        });

        $.ajax({
            url: '<%= url_for :action => "save_permissions" %>',
            data: selected_value,
            dataType: 'json',
            success: function(data) {

                alert("101 Data Successfully Updated");
                $("#rma_options").dialog('close');
            }
        }); //ajax function end
    }

});​
4

2 に答える 2

0

私は次の行で何かを考えていました

var start = {
    "role_name": "bhavin",
    "user_id": "50667afb9de0f2600b000001",
    "Par_user": "user",
    "Child_user_index": "user_index",
    "Child_user_profile": "user_profile",
    "Par_App": "App",
    "Child_App_new": "App_new",
    "Child_App_index": "App_index",
    "Par_App1": "App1",
    "Child_App1_index": "App1_index",
    "Par_App2": "App2",
    "Child_App2_index": "App2_index",
    "Child_App2_new": "App2_new",
    "controller": "users",
    "action": "save_permissions"
};

var end = {};
$.each(start, function(key, value) {
    if (/Par_App/.test(key)) {
        if (!end['App']) end['App'] = [];
        end['App'].push(key);
    }
    else if (/Child_user/.test(key)) {
        if (!end['user']) end['user'] = [];
        end['user'].push(key);
    }
});

console.log(JSON.stringify(end));

ちなみにまだ未完成です。:)ワーキングフィドル

于 2012-10-18T07:19:32.680 に答える
0

JavaScript 関数を使用するJSON.parse()

var jsonString = '{ "role_name": "bhavin", "user_id": "50667afb9de0f2600b000001" }';
var jsonObject = JSON.parse(jsonString);
console.log(jsonObject);
于 2012-10-18T06:18:26.263 に答える