开发者

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 .

will it be

{"rec":{"character":"a"}, "rec":{"ch开发者_如何转开发aracter":"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.


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:

{
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. 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".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜