如何在JSON中创建数组indide数组

时间:2016-12-13 14:15:58

标签: jquery json

我有3个表单字段。我想制作一个JSON数组。我不知道如何使我的要求像JSON数组,只在一个填充内我想再制作一个数组。我正在努力,但我无法创造。

var title = $("#title").val();
var authorities = $(".your_profile option:selected").val();
var edition = $("#edition").val();
response = [];
var activated = true;
authorities=[];
var authorities = $(".your_profile option:selected").val();
var createdDate= "2016-12-13T13:35:24.451Z";   
   response.push({activated:activated,authorities:authorities,createdDate:createdDate});
   console.log(response);
Title:<input type="text" id="title">
authorities:<selected>
	<option value="Agent">Agent</option>
	<option value="Developer">Developer</option>
	<option value="Customer">Customer</option>
</selected>
Title:<input type="text" id="edition">
<input type="button" id="btn-submit" value="submit">

我需要这样的输出

[
    {
        "title": "Professional JavaScript",
        "authorities": [
            "Nicholas C. Zakas"
        ],
        "edition": "3",
    },
]   

2 个答案:

答案 0 :(得分:0)

你可以这样做:

<强> HTML

Title:<input type="text" id="title">
authorities:<select id="selection" multiple="multiple">
    <option value="Agent">Agent</option>
    <option value="Developer">Developer</option>
    <option value="Customer">Customer</option>
</select>
Edition:<input type="text" id="edition">
<input type="button" id="btn-submit" value="submit">

<强> JAVASCRIPT

<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
        <script type="text/javascript">
$(function(){
    $("#btn-submit").click(function(evt){
    //Prevent the default behaviour
    evt.preventDefault();
    var title =$("#title").val();
        var authorities = $("#selection").val();
        var edition =$("#edition").val();
    var response = new Object();
    response.title = title;
    response.authorities = authorities;
    response.edition = edition;
    var stringed = JSON.stringify(response);
    alert(stringed);
  });
});

如果您尝试使用此代码,则会根据需要生成字符串“ stringed

我希望这很有用

安吉洛

答案 1 :(得分:0)

你可以这样做:

  

使用JSON.stringify()将您的数据转换为json

&#13;
&#13;
$(function() {

    $("#submitBtn").click(function(e){
      //Prevent the default behaviour
      e.preventDefault();
      var title =$("#title").val();
      var authorities = $("#selection").val();
      var edition =$("#edition").val();
      var response = new Object();
      response.title = title;
      response.authorities = [authorities];
      response.edition = edition;
      var json_str = JSON.stringify(response);
      console.log(json_str);
    });

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Title:
<br>
<input type="text" id="title">
<br>
Authorities:
<br>
<select id="selection">
    <option value="Agent">Agent</option>
    <option value="Developer">Developer</option>
    <option value="Customer">Customer</option>
</select>
<br>
Edition:
<br>
<input type="text" id="edition">
<br><br>
<input type="button" id="submitBtn" value="submit">
&#13;
&#13;
&#13;

并通过ajax传递数据,你可以这样做:

$.ajax({
   url: 'url_to_web_server',
   type: 'POST',
   data: json_str, // Your json variable here
   type: 'json',
   success: function(data) {
      // Handle success response here
   },
   error: function() {
      // Handle error response here
   }
});

希望这有帮助!