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

使用 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 回答
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 回答