开发者

Can I use a JSON array as a small database?

I'm retreiving a JSON string and parsing it with jQuery with $.getJSON.

After I get the data in a variable, can I add or remove rows? An example:

{
  "one": [{
    "sid": "1",
    "name": "NAME 1"
  }, {
    "sid": "2",
    "name": "NAME 2"
  }],
  "two": [{
    "sid": "3",
    "开发者_开发知识库name": "NAME 3"
  }]
}

Can I delete sid 1 from "one" and place it in "two"? How about sorting by sid? I'm using jQuery.


sure, you can do any of that, they are regular objects and arrays. You can sort arrays, remove their members, add members to other arrays, etc. I wouldn't use jquery stuff, just use basic array tools, such as splice and push and pop. splice() can do most anything: http://www.w3schools.com/jsref/jsref_splice.asp

Sorting is easy as well, just use sort() on the arrray.


Sure, just evaluate the JSON document to JavaScript objects, and work on them.


"After I get the data in a variable" - unless this variable is holding a string, which it probably isn't, it's not really JSON at that point - your question is a bit vague. JSON is an encoded form of data, usually used for sending information over a network or for storage on disk. It's a way to serialize information.

It could be used as the backend storage for a small "database". This kinda depends on your definition of "database" - if you're thinking generic relational database, yes, it can, inefficiently.

Your post however, makes it sound like you got some data and loaded it into a variable, which isn't really "using JSON as a database".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜