简体   繁体   English

如何在ExtJS中为AJAX调用创建JSON?

[英]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. 我想创建此参数文件,以便可以将其发送到Web服务。

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 用于将Javascript对象序列化为JSON字符串,您可以使用

JSON.stringify(Object);

which is available in most of the latest browers, else you can use ExtJS inbuilt method 大多数最新浏览器都可用,否则您可以使用ExtJS内置方法

Ext.encode(Object);

and for deserializing JSON string you can use JSON.parse(JSONString) or Ext.decode(JSONString) 对于反序列化JSON字符串,您可以使用JSON.parse(JSONString)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. 一种简单的方法是将数据创建为javascript对象,然后使用Json“ stringifier”将其转换为json字符串,然后可以将其传递到您的服务器。

This same problem was answered previously at Serializing an object to JSON 先前在将对象序列化为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. 如果您使用jquery(我强烈建议将其作为对所有认真的javascript程序员非常有用的工具),那么有个不错的插件可用于在Ajax调用中来回传递json。 See http://code.google.com/p/jquery-json/ 请参阅http://code.google.com/p/jquery-json/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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