をJSONを作成します。私はこの形式でAPIにポストを経由してJSONを送信する必要がある特定の形式で
"answer" => {
"name"=>"Test",
"email"=>"[email protected]",
"hospital"=>"Hospital Name",
"answered_questions_attributes"=>{
"0"=>{
"value"=>"1",
"question_id"=>"1"
},
"1"=>{
"value"=>"0",
"question_id"=>"2"
},
"2"=>{
"value"=>"1",
"question_id"=>"3"
}
}
}
私は入力から「answered_questions_attributes」データを取得し、値がtrueまたはfalseであり、入力の名前質問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を返す:
var resp_val = jQuery(".resp_val_div").find("input");
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":parseInt(el.value), "question_id":el.name};
dados.answered_questions_attributes.push(d);
});
console.log(dados);
"answer"=>{
"name"=>"Test",
"email"=>"[email protected]",
"hospital"=>"Hospital Test",
"answered_questions_attributes"=>[
{
"value"=>1,
"question_id"=>"1"
},
{
"value"=>0,
"question_id"=>"2"
},
{
"value"=>1,
"question_id"=>"3"
}
]
}
この場合、最初のjsonをどのように作成できますか?
これはJSONではありません。あなたはそれがAPIだと思っていますか?JSONをいくつかの言語(おそらくPHP)で作成する方法の例であるように見えますか? –