1

I want to create this parameter file so that I can send it to my web service.

var parms = {
  "quiz_id":"120",
  "owner_id":"1",
  "solver_id":"1",
   "answers":
    [
      {
  "answer_text" : "YES",
  "question_id" : "1"
      },
      {
  "answer_text" : "NO",
  "question_id" : "2"
      },
      {
  "answer_text" : "YES",
  "question_id" : "3"
      },
      {
  "answer_text" : "YES",
  "question_id" : "4"
      },
      {
  "answer_text" : "YES",
  "question_id" : "5"
      }
   ]
};

I am stuck with the contents inside of the answers. I don't know how to create it dynamically.

wes
  • 7,795
  • 6
  • 31
  • 41
rash111
  • 1,307
  • 4
  • 19
  • 35

3 Answers3

4

for serializing Javascript objects to JSON strings either you can use

JSON.stringify(Object);

which is available in most of the latest browers, else you can use ExtJS inbuilt method

Ext.encode(Object);

and for deserializing JSON string you can use JSON.parse(JSONString) or Ext.decode(JSONString)

Saket Patel
  • 6,573
  • 1
  • 27
  • 36
1

An easy way to do this is to create your data as a javascript object and then use a Json "stringifier" to turn it into a json string, which can then be passed to your server.

This same problem was answered previously at Serializing an object to JSON

If you use jquery (and I highly recommend it as a very useful tool for all serious javascript programmers), there is a nice plugin that I use for passing json back and forth in Ajax calls. See http://code.google.com/p/jquery-json/

Community
  • 1
  • 1
Howard Schutzman
  • 2,115
  • 1
  • 15
  • 8
  • i am using extjs 3.4 for creating json and making ajax call for webservice – rash111 Mar 14 '12 at 05:02
  • I just saw your comment and am not sure why the fact that you are using extjs excludes you from using a stringifier. If you don't want to bother with the JQuery overhead, then the stringifier from json.org is fine. – Howard Schutzman Mar 17 '12 at 18:22
0

Create an object with array and some objects inside and then look at the Ext.data.proxy.Server.encodeFilters() method.

sha
  • 17,824
  • 5
  • 63
  • 98
  • download ExtJs if you haven't yet. go to src/data/proxy. Check Server.js file – sha Mar 13 '12 at 13:56
  • well.. I thought you were using 4 by looking at your tags. You still can download ExtJs 4.0 and see how it's done. Not sure if 3 has similar methods - I don't have much experience with 3 – sha Mar 13 '12 at 14:20
  • download ExtJs 4.0.7. go to src/data/proxy. Check Server.js file – sha Mar 13 '12 at 14:34
  • can i use for loop inside js file – rash111 Mar 13 '12 at 14:52