简体   繁体   English

从表中的数据构造一个json

[英]Structure a json out of data from a table

I have a table with data like so: 我有一个数据表,如下所示:

a

b 

c

d

e

f

..so on till Z

now I need to load this into a JSON and I'm really really confused as to how it will be . 现在我需要将它加载到JSON中,我对它的样子真的很困惑。

will it be 那将会

{"rec":{"character":"a"}, "rec":{"character":"b"}}

or should it all be within an array (this doesn't make sense to me) ? 还是应该全部放在一个数组中(这对我来说没有意义)?

I'm really new to JSON and would really appreciate some quick help. 我是JSON的新手,非常感谢您提供的快速帮助。

For a complicated table: The "correct" approach depends on what the data actually means. 对于复杂的表:“正确”的方法取决于数据的实际含义。 You could use arrays for the key-values: 您可以将数组用于键值:

{"a":[1,2,3], "b":[4,5,6], etc.}

However, it's often better to use sub-keys. 但是,通常最好使用子键。 For instance, say I have a database table like: 例如,假设我有一个数据库表,例如:

RECORD   FIRST  LAST    BIRTHYEAR
     1   Sam    Spade   1977
     2   Jane   Tarzan  1945
     3   Billy  Boinger 1984

To convert the whole table into JSON, I might do: 要将整个表转换为JSON,我可以这样做:

{
1: {
  "first":"Sam",
  "last":"Spade",
  "birthyear":1977
  },
2: {
  "first":"Jane",
  "last":"Tarzan",
  "birthyear":1945
  },
3: {
  "first":"Billy",
  "last":"Boinger",
  "birthyear":1984
  }
}

You can see how that is not only more readable, but it makes it easier to access precisely the data you need. 您可以看到它不仅更具可读性,而且使精确访问所需数据变得更加容易。

If these are values from a select query, I would think you can do it using a JSONArray . 如果这些是来自选择查询的值,我认为您可以使用JSONArray Its string structure would look like this: 其字符串结构如下所示:

["a","b","c",...,"x","y","z"]

It currently cannot be what you have listed, as your (top level) JSONObject contains two of the same key "rec". 它当前不能是您列出的内容,因为(顶级) JSONObject包含两个相同的键“ rec”。

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

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