【问题标题】:How to build this json?如何构建这个json?
【发布时间】:2013-01-23 03:40:40
【问题描述】:
[{"username" : "11"},
      {"password" : "test"},
      {"detailorder" : [
        {"id" : "1",
         "qty" : "5"},
        {"id" : "2",
         "qty" : "10"}
      ]}
    ]

我如何在 javascript 中创建上面的 json?我对json的理解非常低。我参考了How do i build JSON dynamically in javascript?。我需要动态地添加数据,尤其是对于 detailorder。但我从一开始就卡住了。

我写了

var datajson = [];

我不知道接下来要写什么。对不起,我的英语不好。谢谢

【问题讨论】:

  • 这看起来不像是有效的 JSON,因为 orderdetail 有多个对象,它们没有作为数组包含在 [] 中。
  • 但无论如何,JSON JavaScript。如果表达式有效,只需将其分配给一个变量,您就拥有了您的对象。 var datajson = [{"username": "11"},{}...]
  • 确实是 - 无效的 JSON。您可以随时使用JSONLint 等在线验证工具进行检查。
  • 谢谢你看看给定的网址

标签: javascript json


【解决方案1】:

创建数组,将其分配给变量并对其进行字符串化。

方法如下:

var arr = [
   { username:'11' },
   { password:'test' },
   { detilpesanan: [
       { id:'1',jumlah:'5' },
       { id:'2',jumlah:'10' }
   ]}
];

var json = JSON.stringify(arr);

【讨论】:

    【解决方案2】:

    你的意思是:

    var datajson = [
        { "username" : 11 },
        {"password" : "test"},
        {"orderdetail" : 
            { 
                "id": 1,
                "qty": 25           
            },
            { 
                "id": 2,
                "qty": 10           
            }
        }
    ];
    

    添加:

    var datajson = {};
    datajson.username = 11;
    datajson.password = "test";
    datajson.detilpesanan = [];
    datajson.detilpesanan.push({});
    datajson.detilpesanan.unshift({});
    datajson.detilpesanan[0]["id"] = 1;
    datajson.detilpesanan[0]["jumlah"] = 5;
    datajson.detilpesanan[1]["id"] = 2;
    datajson.detilpesanan[1]["jumlah"] = 10;
    
    console.log( datajson );
    

    【讨论】:

    • 我编辑了 json 格式。我需要像stackoverflow.com/questions/4314008/… answer 那样动态地添加数据
    • @justmyfreak 查看添加的答案
    • 但您的答案中的 orderdetail 似乎不是有效的 json。是否可以将 push 和 unsift 更改为 push([]) 和 unshift([])
    【解决方案3】:

    我想提出一些建议以使其更容易。首先,您需要使用 jquery 或任何其他提供 json 解析和 endcoding 的 javascript 库。然后将该结构创建为 javascript 上的标准对象。使用 jquery(或您选择的任何 javascript 库)为您将其编码为 JSON 字符串。

    我多年来一直使用 JSON 格式,但我几乎不记得需要自己写下来。也许有实例,但我认为我没有将它用于实际实现。

    您也可以访问 json.org,下载可用的解析器和编码器。

    希望对你有所帮助。

    【讨论】:

      【解决方案4】:

      你可以看到:http://www.json.org/js.html

      JSON(Javascrtip Serialization Object)是一种序列化对象类型,所以不能先创建对象再序列化这个对象,像这样:

              function createPerson()
              {
                  var persons = new Array();
      
                  for(i=0; i<3; i++)
                  {
                      var details = new Array();
      
                      for(k = 0; k<2;k++)
                      {
                          var det = new persondetail(k,k*2);
                          details.push(det);
                      }
      
                      var p = new person('user'+i,'pdw'+i,details);
      
                      persons.push(p);
                  }
      
                                  //-- serialize object, see console output
                  console.log(JSON.stringify(persons));
      
              }
      
              function person(user, pwd,det)
              {
                  this.username = user;
                  this.password = pwd;
                  this.detilpesanan = det;
      
              }
      
              function persondetail(id, jumlah)
              {
                  this.id = id;
                  this.jumlah = jumlah;
              }
      

      【讨论】:

        猜你喜欢
        • 2020-01-10
        • 2021-12-28
        • 2023-03-04
        • 2021-05-31
        • 1970-01-01
        • 1970-01-01
        • 2013-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多