开发者

Structured text in JSON

I have been looking for a way to capture structured text (se开发者_开发百科ctions, paragraphs, emphasis, lists, etc.) in JSON, but I haven't found anything yet. Any suggestions? (Markdown crossed my mind, but there might be something better out there.)


How about something like this:

[ { "heading": "Foobar Example" },
  { "paragraph":
    [
      "This is normal text, followed by... ",
      { "bold": "some bold text" },
      "etc."
    ]
  }
]

That is:

  • use a string for plain text without formatting or other mark-up;

  • use an array whenever you want to indicate an ordered sequence of certain text elements;

  • use an object where the key indicates the mark-up and the value the text element to which the formatting is applied.


HTML is a well-established way to describe structured text, in a plain-text format(!). Markdown (as you mentioned) would work as well.

My view is that your best bet is probably going to be using some sort of plain-text markup such as those choices, and place your text in a single JSON string variable. Depending on your application, it may make sense to have an array of sections, containing an array of paragraphs, containing an array of normal/bold/list sections etc. However, in the general case I think good old-fashioned blocks are markup will ironically be cleaner and more scalable, due to the ease of passing them around, and the well-developed libraries for full-blown parsing if/when required.


There also seems to be a specification that might accomplish this Markdown Syntax for Object Notation (MSON)

Not sure if for you it's worth the trouble of implementing the spec, but it seems to be an option.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜