rash111
rash111

Reputation: 1317

How do I create JSON in ExtJS for an AJAX call?

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.

Upvotes: 1

Views: 9655

Answers (3)

Saket Patel
Saket Patel

Reputation: 6683

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)

Upvotes: 4

Howard Schutzman
Howard Schutzman

Reputation: 2135

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/

Upvotes: 1

sha
sha

Reputation: 17850

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

Upvotes: 0

Related Questions