Javascript 以特定格式创建JSON

Javascript 以特定格式创建JSON,javascript,json,Javascript,Json,我需要通过post-to api以以下格式发送json: "answer" => { "name"=>"Test", "email"=>"test@test.com", "hospital"=>"Hospital Name", "answered_questions_attributes"=>{ "0"=>{ "value"=>"1", "questi

我需要通过post-to api以以下格式发送json:

"answer" => {
    "name"=>"Test", 
    "email"=>"test@test.com", 
    "hospital"=>"Hospital Name", 
    "answered_questions_attributes"=>{
        "0"=>{
            "value"=>"1", 
            "question_id"=>"1"
        }, 
        "1"=>{
            "value"=>"0", 
            "question_id"=>"2"
        }, 
        "2"=>{
            "value"=>"1", 
            "question_id"=>"3"
        }
    }
}
我从输入中获取“已回答的问题属性”数据,值为真或假,输入的名称为问题ID,例如:

<div class="resp_val_div">
  <input type="hidden" name="1" value="1" />
  <input type="hidden" name="2" value="0" />
  <input type="hidden" name="3" value="1" />
</div>

在这种情况下,如何创建第一个json?

如果需要对象,请不要使用数组和
.push()
。如果希望
属性是字符串而不是数字,请不要使用
parseInt()

    var dados = {
        "name": jQuery("#name").val(),
        "email": jQuery("#email").val(),
        "hospital": jQuery(".answer_hospital").val(),
        'answered_questions_attributes':{}
    };


    resp_val.each(function(index, el) {
        d = {"value":el.value, "question_id":el.name};
        dados.answered_questions_attributes[index] = d;
    });

如果需要对象,请不要使用数组和
.push()
。如果希望
属性是字符串而不是数字,请不要使用
parseInt()

    var dados = {
        "name": jQuery("#name").val(),
        "email": jQuery("#email").val(),
        "hospital": jQuery(".answer_hospital").val(),
        'answered_questions_attributes':{}
    };


    resp_val.each(function(index, el) {
        d = {"value":el.value, "question_id":el.name};
        dados.answered_questions_attributes[index] = d;
    });

这不是JSON。你确定API就是这么做的吗?它看起来更像是一个用某种语言(可能是PHP)创建JSON的示例?这不是JSON。你确定这就是API的作用吗?它看起来更像是一个用某种语言(可能是PHP)创建JSON的例子。非常感谢。这正是我想要的。非常感谢。这正是我想要的。