Ajax调用中的Json数据对象

时间:2016-02-09 14:24:32

标签: jquery ajax

我创建了3个require 'json' # Initial set up, it seems the root keys are always the same looking at your structure products = { 'type' => 'category', 'properties' => { 'name' => 'Alcoholic Beverages' }, 'children' => [] } data = [{ "category" => ['Alcoholic Beverages', 'Wine', 'Red Wine'], "name" => 'Robertson Merlot', "barcode" => '123456789-000', "wine_farm" => 'Robertson Wineries', "price" => 60.00 }] data.each do |item| # Make sure we set the current to the top-level again curr = products['children'] # Remove first entry as it's always 'Alcoholic Beverages' item['category'].shift item['category'].each do |category| # Get the index for the category if it exists index = curr.index {|x| x['type'] == 'category' && x['properties']['name'] == category} # If it exists then change current hash level to the child of that category if index curr = curr[index]['children'] # Else add it in else curr << { 'type' => 'category', 'properties' => { 'name' => category }, 'children' => [] } # We can use last as we know it'll be the last index. curr = curr.last['children'] end end # Delete category from the item itself item.delete('category') # Add the item as product type to the last level of the hash curr << { 'type' => 'product', 'properties' => item } end puts JSON.pretty_generate(products) 元素。每个表单包含8个文本字段。如果单击“提交”按钮,我在这些文本字段中输入的数据将转换为JSON格式,最后发布到我的数据库中。我正在使用AJAX后调用。我的代码是:

form

请更正我的代码。我想知道如何将3个表单的数据转换为单个JSON对象。如果我的方法有误,请发布任何样本。如何实现这一目标?

0 个答案:

没有答案