简体   繁体   中英

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()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM