javascript 在javascript中创建一个Json对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12440350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:12:17  来源:igfitidea点击:

Create a Json object in javascript

javascriptjqueryjson

提问by Umesh Awasthi

I am in process to validate a form where i need to show certain radio buttons and user need to select them based on some rules,how many number of radio buttons can be created is dynamic so i can not do validation on server side not can write a predefined java-script code for that.

我正在验证一个表单,我需要显示某些单选按钮,用户需要根据某些规则选择它们,可以创建多少个单选按钮是动态的,所以我不能在服务器端进行验证也不能写一个预定义的 java 脚本代码。

each of the radio buttons will be divided in to groups say requiredand than further down they can be grouped like center,left, rightetc, so from each group user need to select one value, so the structure comes out like this

每个无线电按钮将在给组说被划分required和比进一步向下它们可以像进行分组centerleftright等从每组用户需要,所以要选择一个值,所以结构出来这样

-Main Group (if block needs to validate based on this e.g if key=required should validate)
  |
  Sub-group (say left, right etc)
   |
   number of radio buttons based on the sub-group

So the main group key can be used to decide if validation should be done on that or not and based on the sub-group key i can decide what all values will be there and needs to be validate

因此,主组密钥可用于决定是否应该对其进行验证,并且基于子组密钥,我可以决定所有值将在那里并且需要验证

i was planning to create a JSON object on page rendering time like

我计划在页面渲染时创建一个 JSON 对象,例如

{"required": [
        {"center": "id1,id2,id3"},
        {"left": "id1,id2,id3"}
      ]
  "optional": [
        {"center": "id1,id2,id3"},
        {"left": "id1,id2,id3"}
      ]
};

i am not sure if the structure i am thinking is right and how to create it in java script? like i have a external loop for key and than one more loop for the sub-group and finally for the buttons in the sub-group,

我不确定我想的结构是否正确以及如何在 java 脚本中创建它?就像我有一个键的外部循环,而不是子组的一个循环,最后是子组中的按钮,

 for(main group key){
     for(subgroup key){
       for(list of radio button under subgroup key)
}
  }

but not sure how to create a right structure so that i can parse it later with jquery and use that for validation.

但不确定如何创建正确的结构,以便我稍后可以使用 jquery 解析它并将其用于验证。

Any help in this will really be appreciated.

在这方面的任何帮助将不胜感激。

回答by Ashirvad

In javascript. You can use JSON.stringify(myObject, replacer);

在 JavaScript 中。您可以使用 JSON.stringify(myObject, replacer);

For example.

例如。

create on javascript object like this

像这样在 javascript 对象上创建

var myObject={};

now after creating javascript object you can convert it into JSON structure like this

现在在创建 javascript 对象后,您可以将其转换为这样的 JSON 结构

var myJsonText=JSON.stringify(myObject);

NOTE: replacer is optional

注意:替换器是可选的

Now if you want to convert it in JSON Object Use JSON.parse method

现在,如果您想将其转换为 JSON 对象,请使用 JSON.parse 方法

myJsonObject=JSON.parse(myJsonText)