北城已荒凉 2008-10-10 15:29 采纳率: 0%
浏览 134
已采纳

在 jQuery [ duplicate ]中序列化为 JSON

This question already has an answer here:

I need to serialize an object to JSON. I'm using jQuery. Is there a "standard" way to do this?

My specific situation: I have an array defined as shown below:

var countries = new Array();
countries[0] = 'ga';
countries[1] = 'cd';
...

and I need to turn this into a string to pass to $.ajax() like this:

$.ajax({
    type: "POST",
    url: "Concessions.aspx/GetConcessions",
    data: "{'countries':['ga','cd']}",
...
</div>

转载于:https://stackoverflow.com/questions/191881/serializing-to-json-in-jquery

  • 写回答

11条回答 默认 最新

  • 7*4 2009-05-26 19:22
    关注

    JSON-js - JSON in JavaScript.

    To convert an object to a string, use JSON.stringify:

    var json_text = JSON.stringify(your_object, null, 2);
    

    To convert a JSON string to object, use JSON.parse:

    var your_object = JSON.parse(json_text);
    

    It was recently recommended by John Resig:

    ...PLEASE start migrating your JSON-using applications over to Crockford's json2.js. It is fully compatible with the ECMAScript 5 specification and gracefully degrades if a native (faster!) implementation exists.

    In fact, I just landed a change in jQuery yesterday that utilizes the JSON.parse method if it exists, now that it has been completely specified.

    I tend to trust what he says on JavaScript matters :)

    Newer browsers support the JSON object natively. The current version of Crockford's JSON library will only define JSON.stringify and JSON.parse if they're not already defined, leaving any browser native implementation intact.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序