如何在jquery中创建字典的JSON数组? [英] How to create a JSON array of dictionaries in jquery?

查看:134
本文介绍了如何在jquery中创建字典的JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html中有一个treeview结构.它只会有节点和子节点.它不能有另一个子级别.该树视图的功能之一是用户可以重新排列节点和子节点.

I have a treeview structure in html. It will only have nodes, and subnodes. It can't have another sublevel. One of the features of this treeview is that users can rearrange the nodes and subnodes.

当用户保存它时,我将使其发送一个JSON对象到我的c#页面,然后在服务器中它将读取新订单并将其保存.该顺序由JSON对象中对象的顺序确定.

When the user saves it, I will make it send a JSON object to my c# page, and in the server it will read the new order and save it. The order is determined by the order of the objects in the JSON object.

我想象JSON对象将如下所示(示例):

I picture the JSON object will look like this (example):

[ {id: 4, items: [5, 6, 7]}, {id: 8, items: [9, 10, 11, 12, ...]}, ... ]

因此它将是一个字典数组.对于每个字典,它将有两个键,id将是节点的ID号,而items将是子节点ID的数组.

So it would be an array of dictionaries. For each dictionary, it will have two keys, the id will be the id number of the node, and the items will be an array of the id's of the subnodes.

重要的是,该顺序必须与我添加到数组中的顺序相同,这样,当我遍历它们时,插入时其顺序仍然相同.如果它像字典一样,那将是不正确的.

It's important that the order is the same as the order I add to the array, so that when I iterate through them, its still in the same order when I inserted them. If its like a dictionary then this wont be true.

我很确定我可以使用jquery创建此字典数组.但是如何将其转换为JSON对象?这可能吗?

Im pretty sure I can create this array of dictionaries using jquery. But how can I turn this into a JSON object? Is this possible?

在c#中,我将如何阅读?

And in c#, how would I be able to read this?

推荐答案

_确实看不到问题.如果使用的是jQuery,则使用的是Javascript. JSON对象 是Javascript对象,因此以下内容在jQuery中有效.它将对象序列化为字符串.

_ really don't see the problem. If you're using jQuery you're using Javascript. A JSON object is a Javascript object, so the following would be valid in jQuery. It will serialize the object into a string.

var data = [ {id: 4, items: [5, 6, 7]}, {id: 8, items: [9, 10, 11, 12, ...]}, ... ];
$.post({ 
     data: data,
     // ...
}); 

如果需要字符串,则JSON.Stringify()会将对象转换为字符串. (我没有意识到它是Java语言的本机).自从我一直使用jQuery ajax以来从未如此.

If you need the string, JSON.Stringify() will turn the object into a string. (I didn't realize it was native to Javascript). Never had too since I've been always using jQuery ajax.

这篇关于如何在jquery中创建字典的JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆