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.


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)


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/


创建一个包含数组和内部对象的对象,然后查看Ext.data.proxy.Server.encodeFilters()方法。

链接地址: http://www.djcxy.com/p/46276.html

上一篇: 将[[object Object],[object Object]]打印到json数组

下一篇: 如何在ExtJS中为AJAX调用创建JSON?