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

javascript - How do I create JSON in ExtJS for an AJAX call? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Mar 17, 2012 at 0:19 wes 8,2356 gold badges33 silver badges41 bronze badges asked Mar 13, 2012 at 13:31 rash111rash111 1,3174 gold badges19 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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)

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 remend 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./p/jquery-json/

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

发布评论

评论列表(0)

  1. 暂无评论