最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Create a Json object in javascript - Stack Overflow

programmeradmin4浏览0评论

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.

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

-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

{"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,

 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.

Any help in this will really be appreciated.

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.

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

-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

{"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,

 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.

Any help in this will really be appreciated.

Share Improve this question edited Sep 15, 2012 at 19:39 Umesh Awasthi asked Sep 15, 2012 at 19:26 Umesh AwasthiUmesh Awasthi 23.6k38 gold badges138 silver badges210 bronze badges 3
  • 3 JSON is Javascript Object Notation, so any JSON is just a native Javascript Object. In fact, most browsers include tools for manipulating JSON (check out the JSON namespace). To remotely load JSON, you should look into jQuery's $.ajax (namely, the JSON format option). With $.ajax, you can easily download JSON with jQuery, and then parse it in your callback function. – neersighted Commented Sep 15, 2012 at 19:40
  • @neersighted:my problem is, i need to create this object on the jsp page itself and not on server side, i know about jquery json and ajax thing, but i need to create the JSON object on the Ui side. – Umesh Awasthi Commented Sep 15, 2012 at 19:42
  • 1 Um, just create a new object? var foo = {}, foo["bar"] = "baz". Or, if you want it to hold specfic information, check out this tutorial. – neersighted Commented Sep 15, 2012 at 19:47
Add a ment  | 

1 Answer 1

Reset to default 5

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

For example.

create on javascript object like this

var myObject={};

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

var myJsonText=JSON.stringify(myObject);

NOTE: replacer is optional

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

myJsonObject=JSON.parse(myJsonText)
发布评论

评论列表(0)

  1. 暂无评论