在j#中将json或multidimentional数组保存到DB [英] Save json or multidimentional array to DB in C#

查看:74
本文介绍了在j#中将json或multidimentional数组保存到DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

返回输出的示例:



[{id:1,children:[{id:2,children:[{ ID:4},{ ID:7},{ ID:8}]},{ ID:3}]},{ ID:5},{ ID:6} ]









i需要输出如





数组



[0] =>数组



[id] => 1

[儿童] =>数组



[ 0] =>数组



[id] => 2

[儿童] =>数组



[0] =>数组



[id] => 4





[1] => A. rray



[id] => 7





[2] =>数组



[id] => 8













[1] =>数组



[id] => 3













[1] =>数组



[id] => 5





[2] =>数组



[id] => 6





Example of returned output:

[{"id":1,"children":[{"id":2,"children":[{"id":4},{"id":7},{"id":8}]},{"id":3}]},{"id":5},{"id":6}]




i need output like


Array
(
[0] => Array
(
[id] => 1
[children] => Array
(
[0] => Array
(
[id] => 2
[children] => Array
(
[0] => Array
(
[id] => 4
)

[1] => Array
(
[id] => 7
)

[2] => Array
(
[id] => 8
)

)

)

[1] => Array
(
[id] => 3
)

)

)

[1] => Array
(
[id] => 5
)

[2] => Array
(
[id] => 6
)

)

推荐答案

您需要的是一个JSON库。你在这里描述的不是一个多维数组,它是一棵树。

大多数都可以反序列化为动态对象,但是如果你可以声明一个可以映射到这个JSON表示的类,那么它会更快。这可能很棘手,因为它是简单对象嵌套的递归。并非所有图书馆都能处理它。



所以你可以尝试至少三种方法:

- 反序列化为动态对象

- 使用正确声明的树节点类(作为root)进行deserailize

- 如果树结构以某种形式保持不变,则使用预定义的类反序列化



试试这些库: fastJSON [ ^ ], Json.NET [ ^ ], APJSON [ ^ ]或其他库。



比你可以解析对象你得到并保存它。

但你也可以考虑其他方法,我建议你在这里查看我的答案:在Web服务器的表格中存储从web方法返回的Json数据 [ ^ ]
What you need is a JSON library. What you described here is not a multidimensional array, it is a tree.
Most of them can deserialize to dynamic object, but if you can declare a class that can be mapped to this JSON representation, than it will be faster. And that can be tricky, since it is a recursion of simple objects's nesting. And not all libraries will be able to deal with it.

So you can try at least three approaches:
- deserialize to dynamic object
- deserailize using a properly declared tree node class (as root)
- deserialize using a predefined class, if the tree structure is constant in some form

Try these libraries: fastJSON[^], Json.NET[^], APJSON[^] or other libraries.

Than you can parse the object you get and save it.
But you could have also other approaches in mind, I suggest you check my answer here: Storing data of Json, returned from a webmethod, in tables of sql server[^]


您的设计过于复杂。

如果您创建一个深度嵌套树,当您从DAL接收数据然后然后需要在Biz层中构建一个对象图再次为JSON做一个组合,将详细信息发送回DB。

这不是解决这个问题(需要专门的JSON库,如上所示),但只考虑一下它将导致的维护你。



所以创建一个简单的对象,它可以包含另一个对象的reference-Id(并且没有引用本身)。如果您创建完整引用,那将创建循环引用。然后将诸如集合之类的对象发送到DB。在内部,您可以将这些作为参考



Your design has been overly complicated.
If you create a deep nested tree, you will be required to build an Object Graph in Biz layer when you receive the data from DAL and then again do a composition to JSON for sending details back to DB.
Its not that this cannot be solved (requires specialized JSON libraries as indicated above), but just think for a while about the maintenance it will cause you.

So create simple object, which can contain a "reference-Id" to another object (and not the reference itself). If you create a full reference, that will create a cyclic reference. Then send such objects as collection to DB. Internally you can have these as references

class person {

private int Id;
public person Parent { private set; public get { return GetPersonFromRepo(parentId); }
public List<person> Children { private set; public get {
List<person> persons = new ....
for(int i=0;i<children.length;>      persons.Add(GetPersonFromRepo(children[i]);
}
return persons;
 }

private int parentId;
private int[] children;
public(int id, int parentId, int[] children)  {
   this.parentId = parentId;
   this.children = children;
   this.Id = id;
} 
</person></person>



< this is =pseudo =don = 请注意语法只需要逻辑>< / xml>>


<this is="" pseudo="" don="t mind the syntax just take the logic></xml>">


我认为你需要一个自我连接表,

create table selfjoinTable( ID,Name,ParrentID)

每个单元格(ID)可以有一个parrent,每个单元格(ID)可以有很多孩子。
I think you need a self join table,
create table selfjoinTable(ID, Name, ParrentID)
each cell(ID) can have one parrent and each cell(ID) can have many childeren.


这篇关于在j#中将json或multidimentional数组保存到DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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